This question already has answers here:
Replace old file with new file
(3 answers)
Closed 6 years ago.
I have files like below:
Filename.txt
Filename.txt.new
Filename2.txt
Filename2.txt.new
I want to remove the "new" keyword for the files ending with "new"
also when we do this, existing file will also have same name, so i want to overwrite the same with the contents of the files ending with "new"
Sadly this is a case where ren *.foo *.bar won't work, but we can do it with a simple loop:
for %x in (*.new) do move /y "%x" "%~nx"
This simply loops over all files that have a .new extension (you could also do the same only for *.txt.new) and renames them. %~nx removes the extension from the name, in this case the .new.
When using this in a batch file you have to double the % signs:
for %%x in (*.new) do move /y "%%x" "%%~nx"
Related
This question already has answers here:
Delete file with specific extension in batch file
(3 answers)
Closed 2 years ago.
I have .blk and .blkx files in the same folders, of wich im trying to delete only the .blk file. Ive attempted examples found on this post as in del "\folder\*.blk" /s. I managed to get the chosen files deleted however the .blkx files were deleted too (other files werent so i assume its an issue with both the file having .blk in their extension).
How can i select only the .blk file?
Edit: I dont know why this post was marked a duplicate but the above pinned post does not resolve the problem (wich i already talked about not working)
You must have SFNs (short filenames) enabled on that drive, and the .blkx extension is mapped to .blk in the short 8.3 filename. Because of this, and because wildcards match both long and short names, *.blk returns both .blk and blkx files. To distinguish between them you'll need to run a for loop, check the actual extension, and delete each .blk individually.
for %%b in (*.blk) do #(if /I "%%~xb" == ".blk" echo del "%%~b")
Remove echo from the above to actually delete the .blk files.
To recurse into subdirectories (like del /s) use for /r instead of for.
The command is written as to be used in a batch file (per the question tag). To run it at a command prompt, instead, replace the double percents %%b with single ones %b.
This question already has answers here:
Batch command to copy files from source to destination [duplicate]
(1 answer)
Copy Contents From Folder Using Wildcard On The Directory
(1 answer)
xcopy wildcard source folder name to destination
(2 answers)
Using a batch file to copy files with a wildcard in the directory path?
(2 answers)
Closed 3 years ago.
I'm trying to write a batch-file to uninstall GoToMeeting.
Currently I use the following to uninstall the current version:
echo UnInstalling GoToMeeting........
"C:\Program Files (x86)\GoToMeeting\13190\G2MUninstall.exe" /uninstall -silent
The problem is, with each install of a new version, the install directory changes. For example, the version is 13190 but the previous time it was 13022 and 12771 before that. So I practically have to add a line for each version and that is really annoying.
Is there a way using a batch-file to make the directory a wild card during the uninstall?
sorry, no wildcard allowed unless in the very last element of a path or filename.
Instead use a for /d loop to list all subdirectories of a certain directory:
for /d %%D in ("C:\Program Files (x86)\GoToMeeting\*") do (
echo the file you look for is "%%D\G2MUninstall.exe"
)
Bonus: if several versions are installed, this finds all of them
This question already has answers here:
How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
(6 answers)
Closed 4 years ago.
I would like to use a .bat file to unzip a ZIP compressed archive file if possible at all. Nothing fancy, I just want to extract the entire archive file to the same location, i.e. download a .zip file to desktop and want to extract it next to desktop with the same name.
I tried this, but with no success.
for /R "C:\Users\Desktop\test.zip" %%I in ("*.zip") do(
"%ProgramFiles(x86)%\7-zip\7z.exe" x - y -o"%%~dpnI" "%%~fI"
)
exit
You shouldn't need the loop. Depending on whether you want to extract the directory structure contained within the archive or just extract everything to a single directory, you would use:
7z e C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
or
7z x C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
See https://sevenzip.osdn.jp/chm/cmdline/commands/index.htm for a list of commands and drill down as needed for the various options.
You should not need a for loop in your batch file, unless you intend to only extract files based on a list of patterns.
This question already has an answer here:
Batch file creating another batch file, how to ignore commands when writing lines?
(1 answer)
Closed 7 years ago.
So, on school we need to make this assignment.
So we tried to make a bat file that make's some folders and dox.
but the problem is: we want to let the bat file make another bat file.(with commands in it)
but if we write it like:
echo. #echo off F: tree/f>menu.bat
this creates a .bat file that doesn't work. probebly cause we didnt add lines. so how to do this?
how do we fix this?
looking forward to it.
You can use the >> operator to add lines to existing files. One > by itself will create a new file (or overwrite the contents of an existing file).
echo #echo off >menu.bat
echo F: >>menu.bat
echo tree /f >>menu.bat
In a Windows batch file that I inherited and have to edit, there's this line (and I'm simplifying for readability):
FOR %%m in (*.XML) DO IF EXIST D:\DATA\%%~m COPY D:\DATA\%%~m subdir
Which copies XML files in D:\Data to the subdir subdirectory of the current folder.
My question is what does %%~m to that %%m wouldn't do?
The question has been answered in the comments from #Stephan, #rojo, #Magoo.
Summarised here so the question is marked as answered:
The ~ character in %%~m removes the surrounding quotes from the variable m.
If it is a file name that contains spaces you can put the quotes back around the whole path name, for example:
if exist "D:\Data\%%~m" copy "D:\Data\%%~m" subdir
You can learn about these substitutions from for /?. The variable substitution can contain other operations, for example %%~nm and %%~tm to exatract the file name or datestamp.