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.
Related
I am new to batch files but was looking to create one that would use the tree command to write a .txt file of the directory in which the batch file is located and then convert that .txt file to a Word file or PDF that the every day user could view.
This was my attempt at the first part of the process;
tree %~dp0 > %~dp0/"Folder Contents.txt" /A /F
but this gave me an "Invalid Path" message in the .txt file
If anyone could point me in the right direction it would be greatly appreciated.
Thanks in advance for your help.
JosefZ has placed the correct command in the comments but I thought I would add a bit of an explanation here:
Think of how you path would look like with your command. It would be like C:\PathTo\Batch\/"FolderContent.txt" which actually is not a valid path.
%~dp0 will end with a \ so the / in the command from the question is syntactically incorrect.
Further the double quotes should be placed around the total path as JosefZ did in his comment. If you do not do that the path will contain these quotes and will be invalid.
JosefZ further added double quotes and a single . around and at the end of %~dp0 after your tree command. The quotes are there to include possible whitespaces into your folder-path and the . to include folders with subfolders as well. You will get an error message if you do not.
Last thing is that he placed the output file at the beginning of the command and got rid of spaces. You can basically place your output file anywhere however. Although you should notice that you should not contain spaces between > and your output-file-path.
I have a parent folder that contains multiple folders within it. Then, each of these nested folders contains 4 files that make up a GIS shapefile and have different extensions (i.e., ".dbf", ".prj", ".shp", and ."shx"). I am new to coding (outside of R) and do not know whether this can be automated with Python or if I need to run a shell script (I'm working on a windows). I have very rudimentary coding schools so documentation would be great (and/or suggestions of "dummy" sites to read).
Here is an example of the current file structure (showing the four files I want to rename with the subfolder name):
Parent Folder: "Raptors"
Subfolder: "Falco_peregrinus"
File 1: "ra03310.dbf"
File 2: "ra03310.prj"
File 3: "ra03310.shp"
File 4: "ra03310.shx"
Here is what I would like the four files to be renamed to:
File 1: Falco_peregrinus.dbf
File 2: Falco_peregrinus.prj
File 3: Falco_peregrinus.shp
File 4: Falco_peregrinus.shx
Thanks.
For a batch file solution
#echo off
for /d %%a in ("c:\...\Raptors\*") do ren "%%~fa\*.*" "%%~na.*"
For each folder inside the parent one, rename all the files inside the folder to the name of the folder but keeping the extension
for command is used to iterate over the list of folders (/d) under the parent folder. For each of the folders, the replaceable parameter %%a will hold a reference to the subfolder and the code in the do clause is executed for each one.
The code in the do clause executes a ren command, for all the files under the subfolder (%%~fa is the folder being processed with full path), changing its name to the name of the folder (%%~na).
edited The answer is not completely correct. While the basic idea of using only one ren command to rename all the files under each folder is probably the fastest way, the way ren command handles wildcards makes this code fail if the folder name contains dots. To be sure the code will not fail, it is necessary to iterate over the files, renaming each one
for /d %%a in ("c:\...\Raptors\*") do for %%b in ("%%~fa\*") do ren "%%~fb" "%%~nxa%%~xb"
For each folder (%%a), for each file inside the folder (%%b), rename the file to the name of full folder name (%%~nxa) with the extension of the file (%%~xb)
You could use almost any programming language (probably including R) to do this. Python is a good choice here because it has such friendly syntax.
A extremely simple script that will solve your problem might look like this
import os
import os.path
'''
Given a file name, returns a pair with the name and extension (hello.txt => [hello,txt])
'''
def split_name(file_name):
return file_name.rsplit('.',1)
'''
Recursively renames files in subdirectories of base_directory so each file is named the subdirectory name plus the extension
WARNING! You will be very sad if you have multiple files with the same extension in any of those folders
def rename_file(base_directory):
#Get the folder name for base_directory (c:\users\foobar => foobar)
directory_name = os.basename(base_directory)
#List the files in base_directory
for path in os.listdir(base_directory):
old_name = base_directory + '/' + path
#If the path points to a file, rename it directory name + '.' + extension
if os.path.isfile(old_name):
new_name = base_directory + '/' + directory_name + '.' + split_name(path)[1]
if not os.path.exists(new_name):
os.rename(old_name,new_name)
else:
print("ERROR:"+new_name+" exists")
else:
#If it's a subfolder, recursively call rename files on that directory.
rename_files(old_name)
Also, I stongly suggest Learn Python The Hard Way by Zed Shaw and Dive Into Python by Mark Pilgrim
a simple windows command can solve your problem. Read HELP FOR and the try this in the Windows command line:
for /d %a in (*) do #for %b in (%a\*) do #ren %a\%b %a%~xb
let's analyze it
the first for will iterate over all (*) the directories /d and for each found, passed in %a the second for will iterate over all the files it contains (%a\*) and for each file found %b it will do rename ren it %a\%b with the name of the folder it is contained in %a keeping the same extension it had %~xb.
This can be done with batch only, I publish this script only to demonstrate how easy this is in Ruby
# enumerate all subfolders of raptors
Dir.glob("raptors/**/*/") do |folder|
# remember the prefix
pre = File.basename(folder)[/.+_/]
# enumerate all files under this folder
Dir.glob("#{folder}*.*") do |file|
File.rename(file, "#{File.dirname(file)}/#{pre}#{File.basename(file)}")
end
end
There is another answer with python code, this code changed my GIS file names based on folder name very well:
Thanks #Martin Evans.
I am trying to create a batch file (filehandling.bat)
I have a file in C:\Users\username\Downloads called "hita_2013_11_05_19_11_38.csv" where the date/time changes according to the date/time it is created
I need to rename this file to "hita.csv"
The problem comes because their will always be an existing "hita.csv" file in the directory that needs to remain there unless overwritten with the above file so the REN function isn't working because it is not overwriting the file
I also need it to make no changes(do nothing) to the existing "hita.csv" file if the "hita_2013_11_05_19_11_38.csv" file doesn't exist.
I've tried the following commands and can't get any to work: REN, MOV, ROBOCOPY /MOV
I've also tried:
IF EXIST "C:\Users\username\Downloads\Hita*.csv" (
DEL C:\Users\username\Downloads\Hita.csv
REN "C:\Users\username\DownloadsHita*.csv" Hita.csv
) ELSE (
Echo The file was not found.
)
but this command still deletes the "hita.csv" file for some reason
Change your test to IF EXIST "C:\Users\username\Downloads\Hita_*.csv" (note the underscore before the '*'). The * wildcard matches 0 or more characters, so it will match Hita.csv, Hita_01.csv, or HitaXYZ.csv.
Adding the underscore makes it only match files starting with Hita_ instead.
Your hita.csv matches the wildcard search of Hita*.csv, so it will delete it.
I believe you want your wildcard search to be Hita?*.csv. The question mark should act as an "exactly one", while the asterisk is "zero or more" characters, which means Hita.csv will not match that criteria.
I have a script that changes particular string within files names (the file stores in "my_folder"):
Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next
the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.
for example if "my folder" contain:
hello (folder)
hello_.epf (file)
then the script will eventually change the file name to:
hello_v0_1_v0_1._epf (unwanted result)
and i want the result to be:
hello_v0_1._epf
I'll appreciate quick help in this manner.
thanks.
I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.
Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.
for %F in ("g:\my folder\*._epf") do #ren "%F" "%~nF_v0_1%~xF"
If you want to run the command within a batch script, then you need to double all percents.
#echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"
EDIT
The above will append a new version suffix to each file name, before the extension.
If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.
ren "g:\my folder\*_v0_1._epf" "*v0_2.*"
The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.
See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.
MY QUESTION:
I have the same situation as Rishi. I have a number of versions of the same song by the same artist that appear on different CD's. If I use the batch command as written, will DOS overwrite songs with the same name, even if the file size is different for each unique file?
PREVIOUS QUESTION: DOS command to move all files in subdirectories one level up
REFERENCE Rishi asked the question on Jan 15th:
"I have a folder with a lot of sub folders with one or more files in each. I am trying to write a batch file that moves all those files to C:\songs (for example).
C:>FOR /R C:\Test %i IN (*) DO MOVE %i C:\Songs
The folders Test and songs exist, but I get an error saying
%i was unexpected at this time.
What am I doing wrong?"
ANSWER WAS
"FOR /R %i IN (C:\Test*) DO MOVE "%i" C:\Songs
In a batch file, it has to be %%i. Weird quirk of batch."
Within a given folder there can only be one version of a file with a given name. When executed within a batch, the MOVE command will automatically overwrite any pre-existing file of the same name. So the answer to your question is - YES, a file with the same name will be over-written, even if it has a different file size. (Note - if you are using Windows XP then you are not using DOS)
You can prevent a batch move from overwriting an existing file by piping N to MOVE with the -y option:
echo n | move /-y "%%~i" "C:\songs\"
If you want to copy and preserve both versions into the same folder, then at least one version will have to be renamed. You will have to decide what kind of naming scheme you want to use before you can begin coming up with a solution.