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

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%

Related

set variable in forfiles in for cycle

#Echo On
FOR %%f IN (*.jpg) DO (
forfiles /M "%%f" /C "cmd /V:ON /c set fn=#ftime"
echo %%fn%%
)
pause
I want to get #ftime in FOR loop, but this isn't working. Maybe there is another way to get modify time of the file?
In your method, you are setting a variable fn within the cmd instance that is opened by forfiles, but this variable is no longer available in the cmd instance that runs your script.
You can use the ~t modifier of the for variable (so %%~tf in your code) to get the modification date and time, then split off the time portion by substring expansion (see set /?), if the featured resolution of minutes is sufficient (the "%%~nxF" portion just precedes the returned time with the current file name):
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%F in ("*.jpg") do (
set "FTIME=%%~tF"
rem The following line depends on region settings:
echo "%%~nxF": !FTIME:~11!
)
endlocal
pause
Alternatively, you can use a for /F loop to split off the time part from the date:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%F in ("*.jpg") do (
for /F "tokens=1,* delims= " %%I in ("%%~tF") do (
echo "%%~nxF": %%J
)
)
endlocal
pause
If you require a resolution of seconds as supported by forfiles, you need to echo the #ftime value within forfiles and capture that by a for /F loop, which iterates once only per each file (time) (#file returns the current file name, which is then held by "%%~K"):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%F in ("*.jpg") do (
for /F "tokens=1,* delims=|" %%K in ('
forfiles /M "%%~F" /C "cmd /C echo #file^|#ftime"
') do (
echo "%%~K": %%L
)
)
endlocal
pause
Depending on your application, you might not need a separate for loop to walk through *.jpg files, because forfiles could do that on its own:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1,* delims=|" %%K in ('
forfiles /M "*.jpg" /C "cmd /C echo #file^|#ftime"
') do (
echo "%%~K": %%L
)
endlocal
pause
You cannot set variables in your batch file from a forfiles call because that one will always spawn a new shell. forfiles is a program, not a built-in command.
However, you can get the modification time with %%tf in your loop, without forfiles:
setlocal enabledelayedexpansion
FOR %%f IN (*.jpg) DO (
set "fn=%%~tf"
echo !fn!
)
If you need exactly FORFILES, just write output to tempfile:
...
set file=temp.txt
...
FORFILES /M "%%f" /C "cmd /c echo #ftime >> %file%"
FOR /F "usebackq tokens=1,*" %%a In ("%file%") Do (Set fn=%%a | echo %fn%)
DEL %file% /q

XCopy is not working

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\"

get output of different commands on one line

I'm having the below script to display some data on-screen, using one small CMD file:
#echo off
set description=%1
ver
date /t
time /t
echo %description%
This provides output, similar to:
Microsoft Windows [Version 6.1.7601]
ma 08/12/2014
17:00
sometext
But, I would like to have this output:
Microsoft Windows [Version 6.1.7601] [ma 08/12/2014 17:00] sometext
I can't think of easy coding (without including variables and/or temporary files) to have something like that achieved. Is it possible in an easy manner (like using backticks on Linux/Unix) ? How ?
As a bonus, it would be even more better if that one line does not end with a return character. That way, I can add additional text (also in 1 line format) to the file, just using ECHO. Knowing that ECHO itself will add a return eventually. I know I can do this, using Cygwin's PRINTF, but if possible I would like to avoid Cygwin, and use standard CMD.
EDIT: changed the question to include the "ver" command so that it matches the title : output of real commands, to be converted into 1 line
#echo off
set myVar=
set description=%1
for /F "delims=" %%I in ('ver') do (set myVar=%%I)
for /F "delims=" %%I in ('date /t') do (set myVar=%myVar% %%I)
for /F "delims=" %%I in ('time /t') do (set myVar=[%myVar% %%I])
set myVar=%myVar% %description%
echo %myVar%
How do I get the result of a command in a variable in windows?
No temporary file, no visible auxiliary variable (cf. no change to %xyz% variable formally used in the :echo subroutine). Simple to change echo anything to call :echo anything
#ECHO OFF >NUL
:: unrem next line to see no change
:: set "xyz=XYZ"
Call :echo %time%
Call :echo after 5 secs
ping 1.1.1.1 -n 1 -w 5000 > nul
Call :echo %time%
Call :echo another 5 secs
ping 1.1.1.1 -n 1 -w 5000 > nul
Call :echo %time%
:: add new line in next ECHO. command
echo.
:: ensure variable xyz is not defined / changed
set xyz
:: and STDOUT command output
for /F "tokens=* delims=" %%G in ('ver /?') do call :echo %%G
for /F "tokens=* delims=" %%G in ('ver') do call :echo %%G
goto :eof
:echo
<nul (set/p xyz=%* )
goto :eof
And to see your task solution:
#ECHO OFF >NUL
for /F "tokens=* delims=" %%G in ('ver') do call :echo %%G
for /F "tokens=* delims=" %%G in ('date /t') do call :echo %%G
for /F "tokens=* delims=" %%G in ('time /t') do call :echo %%G
call :echo sometext
call :echo
goto :eof
:echo
if "%*"=="" echo.&goto :eof
<nul (set/p xyz=%* ) 2>NUL
goto :eof
In last code snippet adhered to rule only one command for the same task: echo. replaced by call :echo to add new line to output. However, let echo ON and echo OFF apart...

Space in batch file path

I'm trying to run a simple batch file.
for /f %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
this only works if there is no space in the path of the batch file.
the batch is in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\vorace.exe
the files I want to loop through are in:
C:\Users\Rafael\Documents\Visual Studio 2012\Projects\vorace\Release\EnsembleIndependant
can anyone help me.
thanks.
thank you guys for the suggestions.
for /f "delims=" solved my problem.
for /f "delims=" %%a IN ('dir /b /s "%~dp0EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
%~dp0 has already a backspace at the end.
for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)
space is a standard delimiter in batch for loops as well as <tab> and with "delims=" you deactivate it.
FOR /F uses as standard delimiters space and tab, to avoid this you need to add the options.
for /f "delims=" %%a IN ('dir /b /s "%~dp0\EnsembleIndependant\*.mis"') DO (ECHO %%a >> ResultatVorace.txt & CALL vorace.exe -f %%a >> ResultatVorace.txt)

Setting a start position in a Path(Batch processing)

i'm a newbie in batch scripting,started learning this from last week only and this is the first question that i'm asking here.here is my situation,
Consider this example,this lists all directories under D:/Jose/test1 and append this to a text file.
Code:
#echo off
SETLOCAL EnableDelayedExpansion
cd /d D:\Jose\test1
FOR /F "delims=" %%G in ('dir /ad /on /s /b') DO (
ECHO %%~pG%%~nG>>D:\test2\list.txt
)
ENDLOCAL
pause
Text file output :
\Jose\test1\1
\Jose\test1\2
\Jose\test1\1\12
\Jose\test1\1\13
\Jose\test1\1\12\131
\Jose\test1\1\12\Copy of 131
\Jose\test1\1\12\131\1311
\Jose\test1\1\12\131\1311\13111
\Jose\test1\1\12\131\1311\13112
\Jose\test1\1\12\Copy of 131\1311
\Jose\test1\1\12\Copy of 131\1311\13111
\Jose\test1\1\12\Copy of 131\1311\13112
\Jose\test1\1\13\132
\Jose\test1\1\13\132\1321
\Jose\test1\1\13\132\1321\13211
I want to remove '\jose' from all line ie, i want to set '\test1' as the starting path. Need help guys..Thanks in advance...
try it with sed for Windows:
for /d /r %%G in (*) do sed -r "s/^\\[^\]+(\\.*)/\1/" "%%~pnxG">>D:\test2\list.txt
..solution with pure batch:
#echo off &setlocal
(FOR /f "tokens=2* delims=\" %%a IN ('dir /ad /on /s /b') DO ECHO(\%%~b)>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE
..and more batch:
#echo off &setlocal
(FOR /d /r %%G in (*) DO (
SET "fname=%%~G"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "fname=!fname:*\Jose=!"
ECHO(!fname!
ENDLOCAL
))>D:\test2\list.txt
TYPE D:\test2\list.txt
PAUSE

Resources