XCopy is not working - windows

I am trying to figure out why this code isn't working. I created something similar that works fine but this isn't working and not sure why. Does anyone know why?
#echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a-%%b)
xcopy %ThisService_RootDirectory%"saves\*.*" %ThisService_RootDirectory%"backups\worlds\%mydate%_%mytime%" /E /I /Y

Your quotes are off in the XCopy command to where they are in the middle of your path instead of at the start and end.
Update it to this:
REM Create the directory.
MKDIR "%ThisService_RootDirectory%backups\worlds\%mydate%_%mytime%"
REM Copy the files.
xcopy "%ThisService_RootDirectory%saves\*.*" "%ThisService_RootDirectory%backups\worlds\%mydate%_%mytime%" /E /I /Y
Your variable %ThisService_RootDirectory% should not be quoted in the respective SET statement since you are appending subdirectories to it.
For example:
REM Set this way [quotes around then entire declaration].
SET "ThisService_RootDirectory=C:\path\to\user\8\"
REM Do NOT set this way [quotes around just the path].
SET ThisService_RootDirectory="C:\path\to\user\8\"

Related

Passing a Batch Variable Into a Nested Loop Correctly - Delayed Expansion of Variables

I'm attempting to make a script that creates a set of folders from list.txt (a list of file directory names separated by carriage returns), with each folder containing a particular number of subdirectories named by the user.
This script does exactly that and works correctly:
#echo off
setlocal enableextensions
set /P q="How many subdirectories would you like to add in each folder?[0/1/2/3]"
if /I "%q%" EQU "0" goto :somewhere0
if /I "%q%" EQU "1" goto :somewhere1
if /I "%q%" EQU "2" goto :somewhere2
if /I "%q%" EQU "3" goto :somewhere3
if errorlevel 1 goto :problem
:somewhere0
echo All done! Press any key to exit.
pause
exit
...(code cut for brevity)
:somewhere3
set /P c="What is the name of the first subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%c"\"%%d"
set /P c="What is the name of the second subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%c"\"%%d"
set /P c="What is the name of the third subdirectory?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO if not exist %%d md "%%d" cd "%%d" md "%%c" cd ..
if errorlevel 1 goto :problem
goto :somewhere0
...
You get the picture. However, I'm attempting to do this in a loop instead of hard-coding each instance. The code I've developed looks like this:
#echo off
setlocal enableextensions
set "var=string"
set /P q="How many subdirectories would you like to add in each folder?"
for /l %%x in (1, 1, %q%) do (
set /P c="What is the name of subdirectory %%x?"
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d\%c%"
)
Trying to echo the variable %%d or %c% in the middle of the loop doesn't yield anything. Stranger, the script actually creates the top level directories - ie, this code runs
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d
but the subdirectories do not show up. Suggestions?
Note that I don't honestly know why the first working code demands this particular order of % signs, but that is the only way I could get it to work.
enabledelayedexpansion is what you want here.
#echo off
setlocal enableextensions enabledelayedexpansion
set "var=string"
set /P q="How many subdirectories would you like to add in each folder? "
for /l %%x in (1, 1, %q%) do (
set /P c="What is the name of subdirectory %%x? "
for /F "tokens=1 delims=," %%d IN (list.txt) DO md "%%d\!c!"
)
from cmd.exe if you run setlocal /? you will get some more help, but in short, enabledelayedexpansion enables delayed environment variable expansion. It will cause variables within a batch file to be expanded at execution time rather than at parse time. Can also be enabled as CMD /V:ON on commandline, and not in Batch.
You basically tell the command processor to delay the resolution of variables until it executes them. % is substituded with ! to show which variables are to be used in the expansion.

for /f "usebackq" with spaces

I am trying to put the a .jar file path into a var... but It's killing me!
I have problems with spaces.... I tried all combinations possible with `'ยด" and no lucky...
Can't pass "%programfiles(x86)%\java" as parameter to "where" command :(
Obviously I'm not putting well the special characters, but not discover how to do it!
Also checked all options in:
Batch File: FOR /F doesn't work if path has spaces
for /f "usebackq" %%f in (''where /r '%programfiles(x86)%\java file.jar'') do set "jarpath=%%f"
for /f "usebackq" %%f in ("where /r `%programfiles(x86)%\java`" file.jar) do set "jarpath=%%f"
for /f "usebackq" %%f in (`where /r` "%programfiles(x86)%\java" file.jar) do set "jarpath=%%f"
I got it! if you need...
for /f "usebackq tokens=*" %%f in (`where /r "%programfiles(x86)%\java" file.jar`) do set jarfile=%%f

Windows batch operation for adding pdf page dimensions to filename?

I need to include page size information of many single-paged pdf's into their filenames. E.g. "150x250mm.pdf". I found no renamer apps able to do it. I suspect this could be done using a batch file and pdfinfo.exe (from xpdf suite), but I have no idea how to make use of it.. Could you give me some hints?
Yes, you can convert from postscript points to MM. In this case, the script is in the top level folder containing the PDF's to be renamed. It does go into subfolders. If you don't want or need that, remove the /s from the dir command on the 5th line. Change the paths as needed.
#echo off
setlocal enabledelayedexpansion
set "pdfi=U:\Scripts\Utilities\xpdf\pdfinfo.exe"
for /f "delims=" %%a in ('dir /b /s *.pdf') do (
for /f "tokens=3,5 delims= " %%b in (
'%pdfi% -meta "%%a"^|find /i "Page size:"') do (
set pts=%%b %%c
for %%d in (!pts!) do (
call :Eval %%d*.352777778 mm
set "mm1=!mm1!x!mm!"
)
ren "%%~dpfnxa" "!mm1:~1!.pdf"
set mm1=
)
)
exit /b
:Eval <in> <out>
setlocal
if exist eval.vbs del eval.vbs
>eval.vbs echo wsh.echo formatnumber(eval("%1"),0)
for /f "delims=" %%a in (
'cscript //nologo eval.vbs'
) do endlocal & set %~2=%%a
del eval.vbs

batch script delete file containining certain chars

I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"

How do I create a folder using the date and time?

I'm trying to do a quick and simple game backup script and it's not working for me. Here's what I have.
#echo off
:main
RD /S /Q "C:\Users\Citadel\Desktop\Minecraft_Backups"
mkdir "C:\Users\Citadel\Desktop\Minecraft_Backups\%date% - %time%"
XCOPY "C:\Users\Citadel\AppData\Roaming\.minecraft\saves" "C:\Users\Citadel\Desktop\Minecraft_Backups\%date% - %time%" /D /E /C /R /I /K /Y
echo %date% - %time% - Backup Complete >> log.txt
PING 1.1.1.1 -n 1 -w 900000 >NUL
goto main
Honestly the mkdir command was a shot in the dark, but nothing so far has worked so I tried it.
The problem is that %date% and %time% contain special characters that can't be used in directory names. Try this at the top of your script:
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
Or if you prefer 24-hour time, change the second line to:
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
And then use %mydate%_%mytime% in place of %date% %time%.
Note that this may have regional issues, but if you confirm it working for your machine, for local backups it will always be fine.
Here's what I found that seems to work:
#setlocal enableextensions
set datestamp=%date:~-4,4%%date:~-10,2%%date:~7,2%
mkdir %datestamp%

Resources