0

I made a batch script that duplicates the file I specify when opening the file via the command line. This is the script:

setlocal
set file=%1
for /f %%i in ("%file%") do (
set drv=%%~di
set path=%%~pi
set ext=%%~xi
set name=%%~ni
)
copy "%1" "%drv%%path%%name% - Duplicate%ext%"
pause

And this is the command I use:

D:\> "duplicate.bat" D:\testfile.txt

This example works perfectly, but when I have a space in the file name, for example:

D:\> "duplicate.bat" D:\test file.txt

The batch file reads the file name as test and the extension is left blank.

Hopefully this is possible :)


Ok, so thanks to madhawa priyashantha for helping with that problem, but now I realise that if I wan't to hide the cmd window that pops up when I run the script, I can use this solution here, but now the script fails to work on files with spaces again. Hopefully this is the last thing I need to do. :)

Community
  • 1
  • 1
Ben Grant
  • 86
  • 2
  • 15

2 Answers2

0

Try. D:> "duplicate.bat" "D:\test file.txt"

user3851589
  • 75
  • 1
  • 7
0

this is tested and working .if you want to pass argument which consist spaces you need to wrap your argument otherwise bat consider next part after space as a next argument

@echo off
copy %1 "%~n1 - Duplicate%~x1"
pause  

now you need to call this bat file like this

m.bat "D:\test file.txt"

and if no spaces,

 m.bat "D:\testfile.txt"
Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58