This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Format date and time in a Windows batch script
(37 answers)
Time is set incorrectly after midnight
(4 answers)
Closed 2 years ago.
Can someone please help me figure out the syntax to add the time to an output file's name? I was able to get the date, but the time isn't working for me. I keep googling it but so far I have come up empty. I have figured out how to add it to the file itself, but I want to be able to see it in the name.
The code below, is just a super simple robocopy command that I have saved as test.cmd.
Everything works, everything except adding the time.
Feel free to steal it if you want. Just make sure you Google the switches so that you know what you're doing. That part is easy to find.
My Code
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\%date:~-10,2%-%date:~-7,2%-%date:~-4,4% Time.txt" /MT:4
exit
I have figured it out!
Here it is if someone else needs it.
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\date %date:~-10,2%-%date:~-7,2%-%date:~-4,4% time %time:~0,2%.%time:~3,2%.txt" /MT:4
exit
Related
This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.
This question already has answers here:
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 3 years ago.
I receive files via FTP that are placed in a folder called Landing. These files arrive in a folder that is randomly named, for example 000174, and the files inside are named Activity.txt.174 for example.
I have created a small script that allows me to reduce the file name to Activity.txt which works perfectly fine when executed within a command shell but when saved as a batch file it will not execute.
The script line is:
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
I have attempted to add various triggers from other scripts to get this working but when I get this working via CMD it still will not execute from a batch file.
Can anyone help please.
Mike
Change
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
to
FOR /R %%f IN (*.txt.*) DO REN "%%f" *.&
https://ss64.com/nt/for_f.html
Use double percent sign before your variable name.
Batch script
#echo off
FOR /R HERE_YOUR_ROOT_PATH %%f IN (*.txt.*) DO REN "%%f" *.&
#echo on
This question already has answers here:
Why does FOR loop command not work in a batch file which works on Windows command prompt?
(3 answers)
Closed 4 years ago.
Due to problem with WINDOWS DEFENDER I need do the following:
for /r "%appdata%\..\Local\MyProg\2.0" %D IN ("*MyProgram.exe") DO "%~fD
It works perfect in COMMAND PROMPT but not in batch-file or cmd-file - why?
How do I make in a excutable file as a Batch-file or ".cmd"?
When running for loops in a batch file, you need to use an additional % in predefined variables. So it should be:
for /r "%appdata%\..\Local\MyProg\2.0" %%D IN ("*MyProgram.exe") DO "%%~fD
I suggest you read up using the well documented help by running for /? from cmdline. You will benefit from it, guaranteed!
A recursive For loop already returns the full path, additionally there's already a system variable for the path %AppData%\..\Local.
For /R "%LocalAppData%\MyProg\2.0" %%A In ("*MyProgram.exe") Do "%%A"
Depending upon your needs, it may be worth checking out the Start command usage too, Start /?. You may find that Do Start "" "%%A" is what you needed.
This question already has answers here:
How to test if a file is a directory in a batch script?
(23 answers)
Closed 6 years ago.
I have a batch file script that needs to verify if the directory that the user enters in is a real directory or path so that I can create it if it dose not exist.
:GetDir
set /p Dir=
if not <is directory?> %Dir% goto:GetDir
if not exist %Dir% mkdir %Dir%
How can I determine if the value given by the user is a directory?
There are millions of things that the user could enter in that is obviously not a directory.
notes for editor:
This note is to be removed after the editor reads it. I apologize ahead of time if I was not supposed to add this note in. Please correct me so I don't do this again if this is the case and I would appreciate being told where to add these types of notes if a similar situation comes up again. I am trying to maintain and recover this question from when I originally asked it in order to fix my bad reputation for asking bad questions on here. This is not a duplicate question because the marked duplicate is in a different scripting language that I am unfamiliar with. I would also like to mention that I did to some heavy research before asking this question on the the issue that I was having. It would also be greatly appreciated if answering this question could be enabled again so that I can answer my own question because I am not sure that anybody else is going to have the answer, and I currently know how to answer it.
How do I test if a file is a directory in a Batch script ?
#echo off
set VAR="C:\Program Files"
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO %VAR% It's a directory
pause
You can use this to test if it is specifically a directory (and not a file)
#echo off
set "file=C:\someDirectory"
if exist "%file%\." echo directory
pause
Here's a way (two ways, really) to determine if a directory exists, and that it's actually a directory and not a file.
set /p "ServerMods=Enter a Directory: "
REM First, make sure the input ends with a backslash (\)
if %ServerMods:~-1% NEQ "\" set "ServerMods=%ServerMods%\
REM Option 1
if not exist %ServerMods% goto :GetDir
REM Option 2
dir %ServerMods% 1>nul 2>nul || (goto :GetDir)
if exist "%ServerMods%\." goto GetDir
I'm a bit of a newbie with batch files, but am looking for a script to delete .txt log files that are older than 30 days... (FWIW, I am on XP SP3...)
I tried to follow the example found here, (including downloading the forfiles.exe from here) and ended up with this:
forfiles -p"D:\SQL Database Back Ups\Update_FmlaManagement_Log" -s -m *.txt -d-30 -c "cmd /c del #FILE"
But I get this error:
Error path not found (3rd argument) line 545
I did a lot of googling, but can't seem to figure out what this error is talking about. Somebody suggested using forfiles.exe in order to get the #variables to work, but I tried that (along with the full forfiles path), and nothing seems to be working...
Can someone at least point me in the correct direction as to what this error message is telling me?
Just to see whether I could, I cobbled a little sumn sumn together to delete files older than 30 days old without using forfiles. Make sure the pushd line is set to go to the appropriate directory. I got the 2592000 by subtracting 01/01/2013 from 01/31/2013.
#echo off
setlocal enabledelayedexpansion
pushd "D:\SQL Database Back Ups\Update_FmlaManagement_Log"
set today=%date:~-10%
call :toEpoch %today%
set /a thirtyDaysAgo=%epoch% - 2592000
for /f "tokens=1,5*" %%I in ('dir /o:d *.txt ^| findstr "txt$"') do (
call :toEpoch %%I
if "%%K"=="" (set file=%%J) else (set file=%%J %%K)
if /i %thirtyDaysAgo% GTR !epoch! del /q "!file!"
)
popd
goto :EOF
:toEpoch date
echo wscript.echo datediff("s", "01/01/1970 00:00:00", "%1")>epoch.vbs
for /f %%x in ('cscript /nologo epoch.vbs') do set epoch=%%x
del /q epoch.vbs
goto :EOF
(VBscript borrowed from Cameron Oakenleaf.)
Admittedly, this won't work obsessively accurately. For instance, if a log file was last modified at noon 30 days ago and you're running this at 8:00 am, the file will probably be deleted even though it still has another 4 hours to go to reach 30 days old. And it's probably not nearly as efficient as forfiles.exe.
*shrug* It was a road less traveled and I traveled it.
EDIT: HerbCSO has another, probably better implementation that makes mine look like a Rube Goldberg-ian solution.
#file is only the File name. It does not include the path. If your batch file is not being executed within your path it will not see the file to delete it because you are in a different working directory.
I can't believe that the error comes from your sample line.
My forfiles (Win7) throws an error for the missing space between -p and the path.
And the text of the error message isn't normal for batch commands.
I don't know any batch command that tells you the line where an error occours, nor which parameter is wrong.
So I believe you got the error message from a completly different source.
Or you have a second forfiles command that do some totally other thing.
Or do you named one of your tests forfiles.bat?
Well, I'm not positive, but it seems the issue might have been a bad forfiles.exe from Microsoft's FTP server. I ended up downloading a second forfiles.exe (for Windows Server 2003) from this guy's site (direct download).
After replacing the original exe in C:\WINDOWS\system32 and getting rid of the copy in my downloads folder, I changed the syntax a little from the dashes to forward slashes as follows:
forfiles /p "D:\SQL Database Back Ups\Update_FmlaManagement_Log" /s /m *.txt /d -30 /c "cmd /c del #FILE"
and this worked perfectly first try!
So I guess the answer is, don't download forfile.exe from Microsoft's FTP server? :P
EDIT:
I would be very interested to know if other people have successfully used Microsoft's .exe in the link above? Otherwise maybe it's just something with my particular setup...