1

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"
Community
  • 1
  • 1
c.diaz
  • 17
  • 5
  • 1
    Possible duplicate of [Batch file to copy files from one folder to another folder](http://stackoverflow.com/questions/986447/batch-file-to-copy-files-from-one-folder-to-another-folder) – Daniel Brose Mar 24 '17 at 01:24
  • If not duplicate, useful as has reference to constituent parts of similar queries: http://stackoverflow.com/questions/986447/batch-file-to-copy-files-from-one-folder-to-another-folder – Daniel Brose Mar 24 '17 at 01:26
  • `"%%~F"` ... `"%%F"` would be `""C:\Users\my name\some long path with spaces""`. Just delete or `::` comment out the `@echo off` to see what `cmd.exe` does. – AlexP Mar 24 '17 at 01:32
  • Added some further explanation of my query... – c.diaz Mar 24 '17 at 02:48

2 Answers2

1

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"
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thank you for the assist/solution. Neither of these are 100% correct, but this one is closest to my intent. The only change I applied to your solution is to remove the quotes from "%%F". Executed flawlessly. – c.diaz Mar 24 '17 at 15:21
  • @c.diaz, my fault. I missed the quotes. Just to prevent future possible problems I would use `"%%~F"` (that is, a explicit quoted version of the unquoted file reference) – MC ND Mar 24 '17 at 15:43
  • My apologies for speaking too soon. The resultant file copy action was to copy a source file to the destination with the folder name of the source instead of the filename of the source. I spoke too soon as I watched all the correct file names pass by but then in verifying results found the problem. Still working to figure this one out. Trying the solution from MC ND. – c.diaz Mar 24 '17 at 17:07
  • There we have it. A combination of the answers did the trick. Not sure which to mark, but will update the question to include the final solution. Probably going with MC ND considering most similarity in final solution. – c.diaz Mar 24 '17 at 17:10
1

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"