Rename and overwrite files using wildcard in Windows - powershell

I am working on a script for auto deployment, where I need to replace my files with the same filenames.
For example, I have the following files in my current directory
deployment.properties
wrapper.conf
config.properties
Later, I will generate another set of files like this
deployment.properties.tokenized
wrapper.conf.tokenized
config.properties.tokenized
Lastly, I want to replace the existing config files (in the first code block) using the *.tokenized version and remove the tokenized files.
In Linux, the following can do the job. But I don't know how to do in Windows
for f in *tokenized;
do mv "$f" "`echo $f | sed s/tokenized//`";
done
I tried to use powershell's move-item, rename-item but still cannot figure out the right way to do it. Could somebody help? bat / powershell scripts are both welcomed. Using loop is also okay. Thank you.

It is almost the same code but in cmd / batch files we have access to the elements of the file name.
From command line
for %a in (*.tokenized) do move /y "%a" "%~na"
Or, for a batch file (you need to escape the for replaceable parameter)
for %%a in (*.tokenized) do move /y "%%a" "%%~na"
As the extension of the file (the text after the last dot) is .tokenized, when you request just the name (without extension) of the file being referenced (%%~na) you get the original file name.

This PowerShell script should do the job:
Get-ChildItem *.tokenized | % {
move $_.Name ([System.IO.Path]::GetFileNameWithoutExtension($_.Name)) -Force
}

Related

Batch add basename to another file

I screwed up with mmv and deleted a few characters from the end of the filenames in a folder (before the extension).
Luckily I have other files with the same original basename but with a different extension.
I would like to know whether there is a way make a loop:
To match the remainder of the basename from the modified file to the second file with the original filename.
If it matches, rename the file with the original filename
Example:
Wrong filenames: foo-1234.txt
foo-1225.txt
Right files: foo-1234-5678.png
foo-1225-6789.png
Desired output:
foo-1234-5678.txt
foo-1225-6789.txt
Thank you very much in advance!
You didn't mention what environment we are talking about, but I'm assuming it's Windows cmd. You can try this:
#echo off
setlocal enabledelayedexpansion
for %%f in (foo-*.png) do set f=%%f & rename !f:~0,8!.txt !f:~0,13!.txt
It must be executed from batch file - delayed expansion won't work from direct command line.
EDIT
powershell counterpart:
dir foo-*.png | % { $stem = $_.name -replace 'foo-([^.]+).png','$1'; $bad = $stem.substring(0,4); mv foo-$bad.txt foo-$stem.txt; }
bash:
for file in foo-*.png; do stem=${file:4:9}; bad=${file:4:4}; mv foo-${bad}.txt foo-${stem}.txt; done

Find and replace text between certain characters with filename

Find and replace text between certain characters with filename, have about 100 files that require this so I'm thinking it needs a occurrence or something.
Original file name: test1.txt
Inside file replace the following two lines with the file name (inside each file the below strings might not be right after each other):
location000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
Output in file test1.txt
location000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
#echo off
if not exist "%~dp0jrepl.bat" (
echo ERROR: JREPL.BAT missing in directory "%~dp0".
echo/
pause
goto :EOF
)
for %%I in ("C:\Temp\*.txt") do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "%%I" /O -
This batch file works only on NTFS drives. It can result in an endless running loop on FAT16, FAT32 or ExFAT drives or skipping some text files. For a working solution independent on file system replace the last command line by:
for /F "eol=| delims=" %%I in ('dir "C:\Temp\*.txt" /A-D-H /B /ON 2^>nul') do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "C:\Temp\%%I" /O -
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks first if JREPL.BAT really exists in directory of the batch file and if this is not the case, outputs an error message, halts script execution to make it possible for a user to read that error message and then exits. See Where does GOTO :EOF return to?
The command FOR searches in specified directory C:\Temp for non hidden files matching the wildcard pattern *.txt and calls for each found text file the batch file JREPL.BAT to replace the file name between last / and first string starting with ., having one or more characters not being a dot or a comma with next character being a , (= file extension and comma) on lines starting case-sensitive with location000:/ or newlocation000:/ by the file name of the current *.txt file without file extension. So a file name in existing file can contain also one or more . in file name before file extension.
The solution working also on FAT drives uses command DIR to get a captured list of *.txt files with just file name and file extension without path and FOR processes this file names list line by line, i.e. file name by file name. So the list of *.txt files to process does not change on running FOR calling JREPL.BAT as it would be the case on using FOR directly to find the *.txt files on FAT drives.
NTFS returns a list of directory entries matching a wildcard pattern sorted alphabetically and so the list of *.txt files does not modify during FOR iterations in this case. But all FAT file systems return the list of directory entries matching a wildcard pattern according to last modification in directory with last modified file at end of the list. So while FOR gets one file name after the other on using for %%I in ("C:\Temp\*.txt") do from file system and processes the file with calling jrepl.bat which modifies the file, the file list changes on FAT drives and next directory entry returned to FOR on its search for *.txt is either the file just modified (= endless loop) or another file after skipping a file which should be also modified because of file list changed since last directory access by FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains also %~dp0 ... drive and path of argument 0 being the batch file itself.
dir /?
echo /?
for /?
goto /?
if /?
pause /?
jrepl.bat /?

Rename Multiple Text file using Batch file

How would I write a batch file to rename multiple text files?
Suppose we have to rename 200 files as below
ABC_Suman_156smnhk.txt,
ABC_Suman_73564jsdlfm.txt,
ABC_Suman_9864yds7mjf45mj.txt
To
MNC_Ranj_156smnhk.txt,
MNC_Ranj_73564jsdlfm.txt,
MNC_Ranj_9864yds7mjf45mj.txt
Note: I need this ABC_Suman part only changed to MNC_Ranj
Any help would be appreciated.
To perform a batch rename, the basic command looks like this:
for filename in foo; do echo mv \"$filename\" \"${filename//foo/bar}\"; done > rename.txt
The command works as follows:
The for loop goes through all files with name foo in the current directory.
For each filename, it constructs and echoes a command of the form mv “filename” “newfilename”, where the filename and new file name are surrounded by double quotes (to account for spaces in the file name) and the new file name has all instances of foo replaced with bar. The substitution function ${filename//foo/bar} has two slashes (//) to replace every occurrence of foo with bar.
Finally, the entire output is saved to rename.txt for user review to ensure that the rename commands are being generated correctly.
i took it from the following link:
http://www.peteryu.ca/tutorials/shellscripting/batch_rename
#echo off
setlocal enableDelayedExpansion
for %%F in (ABC_Suman*.txt) do (
set "name=%%F"
ren "!name!" "!name:ABC_Suman=MNC_Ranj!"
)

Having XCopy copy a file and not overwrite the previous one if it exists (without prompting)

I'm sending commands to a remote computer in order to have it copy a file.
I want the file to be copied, but not to overwrite the previous file with the same name (if it exists).
I also need the command to run without any prompts (xcopy likes to prompt whether the target name I've specified is file or directory, and it will also prompt about overwriting a file).
I have good results with xcopy /d.
It will copy NEWER files, and since we can assume that existing files have same time-stamp, you will copy only files that don't exist.
just in case anyone else finds this:
robocopy x:\sourcefolder Y:\destfolder /s /e /r:0 /z
much better than xcopy, even gives you a table at the end informing of any failed or skipped files. Doesn't prompt to not overwrite.
Well, there's a certain remedy! It has helped me with saving much of my effort and time on Win10 while writing a setup for our product demo.
Just try to use piping:
#ECHO N|COPY /-Y SourceFiles Destination
As an example I used this piece of code so that I would have a clean gentle quiet and safe copy!
#FOR /D %%F in ("FooPath") DO #(
#ECHO N|COPY /-Y ^"%%~npdxF\*.*^" ^"GooPath^" 3>NUL 2>NUL >NUL
)
where obviously FooPath is the source and GooPath is the destination.
Enjoy!
(main source: https://ss64.com/nt/copy.html)
Following command copy files and folder but not override file if already exist.
xcopy "*.*" "C:\test\" /s /y /d
No way to make it NOT overwrite as far as I know. but /Y will make it overwrite. and /I will get rid of the file/dict prompt. See xcopy /? for all options
You can also use the replace command. It has two modes: to add files that don't exist there or replace files that do exist. You want the previous mode:
replace <path1> <path2> /A
I had to copy AND rename files, so I got the prompt about creating a file or a directory.
This is the, rather "hackish" way I did it:
ECHO F | XCOPY /D "C:\install\dummy\dummy.pdf" "C:\Archive\fffc810e-f01a-47e8-a000-5903fc56f0ec.pdf"
XCOPY will use the "F" to indicate it should create the target as a file:
C:\install>ECHO F | XCOPY /D "C:\install\dummy\dummy.html" "C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html"
Does C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html specify a file name or directory name on the target
(F = file, D = directory)? F
C:\install\dummy\dummy.html
1 File(s) copied
I've also verified this command leaves existing files alone. (You should too :-)

xcopy file, rename, suppress "Does xxx specify a file name..." message

This seems pretty simple and maybe I'm just overlooking the proper flag, but how would I, in one command, copy a file from one directory to another and rename it in the destination directory? Here's my command:
if exist "bin\development\whee.config.example"
if not exist "TestConnectionExternal\bin\Debug\whee.config"
xcopy "bin\development\whee.config.example"
"TestConnectionExternal\bin\Debug\whee.config"
It prompts me with the following every time:
Does TestConnectionExternal\bin\Debug\whee.config specify a file name
or directory name on the target (F = file, D = directory)?
I want to suppress this prompt; the answer is always F.
I use
echo f | xcopy /f /y srcfile destfile
to get around it.
Don't use the xcopy, use copy instead, it doesn't have this issue.
xcopy is generally used when performing recursive copies of multiple files/folders, or when you need the verification/prompting features it offers. For single file copies, the copy command works just fine.
Another option is to use a destination wildcard. Note that this only works if the source and destination filenames will be the same, so while this doesn't solve the OP's specific example, I thought it was worth sharing.
For example:
xcopy /y "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\*"
will create a copy of the file "whee.config.example" in the destination directory without prompting for file or directory.
Update: As mentioned by #chapluck:
You can change "* " to "[newFileName].*". It persists file extension but allows to rename. Or more hacky: "[newFileName].[newExt]*" to change extension
There is some sort of undocumented feature in XCOPY. you can use:
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config*"
i tested it just today. :-)
Just go to http://technet.microsoft.com/en-us/library/bb491035.aspx
Here's what the MAIN ISSUE is "... If Destination does not contain an existing directory and does not end with a backslash (), the following message appears: ...
Does destination specify a file name
or directory name on the target
(F = file, D = directory)?
You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.
Took me a while, but all it takes is RTFM.
So, there is a simple fix for this. It is admittedly awkward, but it works.
xcopy will not prompt to find out if the destination is a directory or file IF the new file(filename) already exists. If you precede your xcopy command with a simple echo to the new filename, it will overwrite the empty file. Example
echo.>newfile.txt
xcopy oldfile.txt newfile.txt /Y
I met same issue when try to copy file with new name only if file does not exist in destination or exist (with new name), but is older. The solution is to add * char at end of destination file name. Example:
xcopy "C:\src\whee.config.txt" "C:\dest\bee.config.txt*" /D /Y
This is from Bills answer.
Just to be really clear for others.
If you are copying ONE file from one place to another AND you want the full directory structure to be created, use the following command:
xcopy "C:\Data\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg" "C:\Target Data\\Images\2013\08\12\85e4a707-2672-481b-92fb-67ecff20c96b.jpg\"
Yes, put a backslash at the end of the file name and it will NOT ask you if it's a file or directory. Because there is only ONE file in the source, it will assume it's a file.
xcopy src dest /I
REM This assumes dest is a folder and will create it, if it doesnt exists
XCOPY with * at the end of the target to copy files whether they exist or not in destination
XCOPY with \ at the end of the target to copy folders and contents whether exist or not in destination
Alternatively
RoboForm SOURCE DEST FILE for files
RoboForm SOURCE DEST for folders
I had a similar issue and both robocopy and xcopy did not help, as I wanted to suppress the comments and use a different destination filename. I found
type filename.txt > destfolder\destfilename.txt
working as per my requirements.
Back to the original question:
xcopy "bin\development\whee.config.example" "TestConnectionExternal\bin\Debug\whee.config"
could be done with two commands eg:
mkdir "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
xcopy "bin\development\whee.config.example" "c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"
By simply appending "\.." to the path of the destination file the destination directory is created if it not already exists. In this case
"c:\mybackup\TestConnectionExternal\bin\Debug\"
which is the parent directory of
the non-existing directory
"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\.."
At least for WIN7 mkdir does not care if the directory
"c:\mybackup\TestConnectionExternal\bin\Debug\whee.config\"
really exists.
The right thing to do if you wanna copy just file and change it's name at destination is :
xcopy /f /y "bin\development\example.exe"
"TestConnectionExternal\bin\Debug\NewName.exe*"
And it's Gonna work fine
I suggest robocopy instead of copy or xcopy. Used as command or in GUI on clients or servers. Tolerant of network pauses and you can choose to ignore file attributes when copying of copy by file attributes. Oh, and it supports multi-core machines so files are copied much faster in "parallel" with each other instead of sequentially. robocopy can be found on MS TechNet.
For duplicating large files, xopy with /J switch is a good choice. In this case, simply pipe an F for file or a D for directory. Also, you can save jobs in an array for future references. For example:
$MyScriptBlock = {
Param ($SOURCE, $DESTINATION)
'F' | XCOPY $SOURCE $DESTINATION /J/Y
#DESTINATION IS FILE, COPY WITHOUT PROMPT IN DIRECT BUFFER MODE
}
JOBS +=START-JOB -SCRIPTBLOCK $MyScriptBlock -ARGUMENTLIST $SOURCE,$DESTIBNATION
$JOBS | WAIT-JOB | REMOVE-JOB
Thanks to Chand with a bit modifications:
https://stackoverflow.com/users/3705330/chand
Place an asterisk(*) at the end of the destination path to skip the dispute of D and F.
Example:
xcopy "compressedOutput.xml" "../../Execute
Scripts/APIAutomation/Libraries/rerunlastfailedbuild.xml*"
Use copy instead of xcopy when copying files.
e.g.
copy "bin\development\whee.config.example"
"TestConnectionExternal\bin\Debug\whee.config"
Work Around, use ReName... and Name it some Cryptic Name, then ReName it to its Proper Name
C:
CD "C:\Users\Public\Documents\My Web Sites\AngelFire~Zoe\"
XCopy /D /I /V /Y "C:\Users\Public\Documents\My Web Sites\HostGator ~ ZoeBeans\cop.htm"
Ren "cop.htm" "christ-our-passover.htm"
xcopy will allow you to copy a single file into a specifed folder it just wont allow you to define a destination name. If you require the destination name just rename it before you copy it.
ren "bin\development\whee.config.example" whee.config
xcopy /R/Y "bin\development\whee.config"
"TestConnectionExternal\bin\Debug\"
When working with single files , I use both commands.
To copy a file to another existing directory, use copy
copy srcPath\srcFile existingDir\newFile
To copy an existing file to and create new directories, use xcopy
xcopy srcPath\srcFile newDirectoryPath\newFile
To suppress the xcopy 'file or directory' prompt, echo in the response. So for a file copy echo in f.
echo f | xcopy srcPath\srcFile newDirectoryPath\newFile
Note flag /y works in both commands to suppress the confirmation to overwrite the existing destination file.
MS Docs: copy, xcopy
Since you're not actually changing the filename, you can take out the filename from the destination and there will be no questions.
xcopy bin\development\whee.config.example TestConnectionExternal\bin\Debug\ /Y
This approach works well when the destination directory is guaranteed to exist, and when the source may equally be a file or directory.
You cannot specify that it's always a file. If you don't need xcopy's other features, why not just use regular copy?
Does xxxxxxxxxxxx specify a file name
or directory name on the target
(F = file, D = directory)? D
if a File : (echo F)
if a Directory (echo D)