Reputation: 17
The linked question may assist in context of my query: Get filename in batch for loop
I'm just not catching on to the rules of substitution, but I think this is a question with a similar answer...
I'm attempting the same sort of move action using the following batch sequence. Anyone able to help me correct the syntax?
@echo off
set source=C:\Users\my name\some long path with spaces
set target=C:\Users\my name\a different long path with spaces
for %%F in ("%source%") do if not exist "%target%\%~nF.jpg" copy "%%F" "%target%\%~nF.jpg"
The idea is to copy all files with no extension where destination with matching filename exists with specific extension into the destination with correct extension. Thanks in advance is anyone is able to assist!
Edit: Thanks for the references Daniel, but I'm not trying to copy a filename to destination with matching extension. I'm trying to copy filename to same filename with new extension
Example:
source\filename001
destination\filename001.jpg
- do nothing
source\filename002
destination\{no match}
- copy source\filename002 to destination\filename002.jpg
Alex, I'm not sure what to do from there. I looked at the output when running without echo off, and that's why I'm posting this question. I don't understand how to modify the substitutions to work correctly.
Output errors from batch:
for %%~F in ("%source%") do if not exist "%target%\%~nF.jpg" copy "%%F" "%target%\%~nF.jpg"
The following usage of the path operator in batch-parameter substitution is invalid: %~nF.jpg" copy "%%F" "%target%\%~nF.jpg"
for %%F in ("%source%") do if not exist "%target%\%~nF.jpg" copy "%%~F" "%target%\%~nF.jpg"
The following usage of the path operator in batch-parameter substitution is invalid: %~nF.jpg" copy "%%~F" "%target%\%~nF.jpg"
SOLUTION: Thanks for the assist/solution/guidance!
set "source=C:\Users\my name\some long path with spaces"
set "target=C:\Users\my name\a different long path with spaces"
for /F "delims=" %%F in (
'Dir /B "%source%\*." '
) do if not exist "%target%\%%~nF.jpg" copy "%source%\%%~F" "%target%\%%~nF.jpg"
Upvotes: 1
Views: 1774
Reputation: 70923
Your problem is that inside batch files the for
replaceable parameters (the variable that holds the reference to the element being iterated) needs to be preceded by two percent signs (%%F
), includig the cases where you use any modifier (ex. file name = %%~nF
).
In command line the replaceable parameter only uses one %
, and your code includes some references written for a batch file and some for command line.
for %%F in ("%source%") do
^^^ for replaceable parameter in batch file, double %
if not exist "%target%\%~nF.jpg" copy "%%F" "%target%\%~nF.jpg"
^^^^ ^^^^
for replaceable parameters missing a % (usage in command line)
So, to solve it
@echo off
set "source=C:\Users\my name\some long path with spaces"
set "target=C:\Users\my name\a different long path with spaces"
for %%F in ("%source%\*.") do if not exist "%target%\%%~nF.jpg" copy "%%~F" "%target%\%%~nF.jpg"
Upvotes: 1
Reputation:
MC ND was a bit faster, but you have to select only files without extension in source so I suggest:
@echo off
set source=C:\Users\my name\some long path with spaces
set target=C:\Users\my name\a different long path with spaces
for /F "delims=" %%F in (
'Dir /B "%source%\*." '
) do if not exist "%target%\%%~nF.jpg" copy "%%F" "%target%\%%~nF.jpg"
Upvotes: 1