I have a list of 4000 .tif images that I need copied from a folder containing 20,000+ images.
I am trying to use command prompt to do so by using code found from googling my problem.
the code is as follows:
for /f %a in H:\list.txt do copy %a H:\new
"H:\list.txt" is my list file
"H:\new" is where i want my files to be copied to
I am running this command in the file folder that contains the .tif files H:\Doc2 and I keep getting:
H:\list.txt was unexpected at this time.
What is causing the issue? Is my list not correctly set up?
It looks like this:
ABBA.TIF
ABBD.TIF
ABBQ.TIF
Do i need commas or colons after the file names?
H:\list.txt is a literal, you want to loop over each of the lines of the file. Check this out:
How do you loop through each line in a text file using a windows batch file?
for /F "tokens=*" %%A in (myfile.txt) do [process] %%A
Suggestion: tag this with "windows" as well.
Amir diagnosed your immediate problem - your code is missing the parentheses around the IN clause.
But you have another potential problem. Your list may include file names and or paths that contain spaces or other special characters. If so, then the name must be quoted.
The values in the list may already by quoted, or they may not. You can use the ~ modifier to strip any existing enclosing quotes that may or may not be there, and then explicitly add your own.
for /f %a in (H:\list.txt) do copy "%~a" H:\new
If you want to include the line in a batch file, then each % must be doubled as %%.
Related
I have a folder with many sub-folders which have further sub-folders. each folder has a number of files.
I want to rename all the file by adding some suffix to them
for ex:
Original: FileName1.ext
Final : Suf_FileName1.ext
To carry out this function, found this command online
`FOR /R %x IN (*) DO ren "%x" Suf_*`
but this replaces the initial characters in the original file name
like
Original: FileName1.ext
Final : Suf_Name.ext
(please note it has removed initial characters in the initial file name )
Please suggest changes/modifications in the above command,
or another command to carry out the function.
Thanks in Advance.
When you use a for command to iterate over a set of files, the for replaceable parameter (%x in your sample) contains a reference to the file.
When you use %x in the commands contained in the do clause, this parameter is replaced by information of the file being processed, by default the name and extension of the file (when using a simple for) or the file with full path (when using a recursive for /R), but you can decide and changee what information you want to use. The replaceable parameter allows the usage of some modifiers (you can see the full list if you execute for /?)
In your case, for your rename mask you need the name and extension of the file being referenced (I've changed your %x with %F so the syntax is clearer)
%~nxF
^^^^.... replaceable parameter
||+..... extension of the file referenced
|+...... name of the file referenced
+....... operator for modifier usage
Your command could be something like
for /R %F in (*) do echo ren "%F" "Suf_%~nxF"
note: The echo in the command is just a debugging habit. Before changing anything, first show to console the commands that will be executed. If the output seems correct then we only have to remove the echo and run the command again.
Remember that if you want to use the command inside a batch file, you need to escape the percent signs in the for replaceable parameters by doubling them. Inside a batch file the previous command will be
for /R %%F in (*) do echo ren "%%F" "Suf_%%~nxF"
Background
I find myself often copying file paths to the clipboard, which is somewhat cumbersome to do from Windows Explorer.
So I wrote a little .bat file to put into the %APPDATA%\Microsoft\Windows\SendTo\ folder utilising the CLIP executable to copy a list of the selected file paths to the clipboard. This file consists only of a single line:
echo|set /p= "%*" | clip.exe
Which works quite nicely, I can select one or more filenames in Explorer, right-click on them and "Send To" the .bat file, which copies them to the clipboard. Each file path is complete and separated from the others by a space character.
Question
Sometimes, I don't want to copy a list of the full file paths, but would prefer to have a list of just the filenames with their extensions. I know how to do that conversion for single file paths, using the %~nx syntax as described here or here.
I tried different combinations of these but can't seem to find a workable solution for my list of paths. The following code echos the filenames correctly:
for %%F in (%*) do echo %%~nxF
...but how do I combine them to pass through to CLIP? Do I have to do string concatenation? Maybe in a subroutine to be called, or is there a more elegant solution?
The following will put each file name on a separate line within the clipboard:
#(for %%F in (%*) do #echo %%~nxF)|clip
If you prefer, the following will put a space delimited list of file names on a single line, with quotes around each file name.
#(for %%F in (%*) do #<nul set /p =""%%~nxF" ")|clip
Couldn't you just:
echo|set /p= "%~nx*" | clip.exe
I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.
I have the following batch statement:
for /f "delims=" %%x in (file.lst) do set "offendingfile=%%x"
Although for some really odd reason, when it is called it outputs:
"C:\Windows\calc.exe "
instead of
"C:\Windows\calc.exe"
Since there is a trailing space, I can't use it properly with any other statements in the batch file, does anyone know why it does this and how to fix this, as its been driving me nuts!
does your file.lst file has a trailing space after the file name?
I checked this with file.lst having: c:\windows\calc.exe and the output was correct, but if the file.lst file contains c:\windows\calc.exe<SPACE>, the output is the same that you are getting (and is the expected output as well).
I believe that the delims= portion of the for statement is removing the default behavior of using spaces as delimiters. If you remove that portion, then it should remove the trailing blank:
for /f %%x in (file.lst) do set "offendingfile=%%x"
I ahve a directory full of files, and I want to rename each of them to have TA_ in front of the original file name. file1.txt should be renamed to TA_file1.txt. What I am getting is TA_e1.txt instead.
ren "c:*.txt" "TA_*.txt" is the command I am trying to use.
The file names are all of various lengths, and no matter what I try, it always overwrites the first 3 characters of my file name....
A simple one liner would be:
for %i IN (*.txt) DO ren "%i" "TA_%i"
This loops over all files (*.txt) and passes their name in the %i variable to the ren command. ren can then use the %i content to expand it with your desired prefix.
The command will only work for files in the current directory. For more complex things you should write a batch file. Come back if you need help with that.