I do have a xcopy statement in bat file..
would you please help me to append today's date to one of directories in destination
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\%destinationFolder%"
today date is 06072013 so I want my destination look like below
C:\Shared\copy-today's date........
Thanks
This is method of getting a date stamp that doesn't depend on the regional settings. Wmic is available in Windows XP Pro and higher.
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%
md "C:\Shared\copy-%stamp%"
xcopy here...
xcopy /S /E /I %sourceFolder% "C:\Shared\copy-%date:/=%\%destinationFolder%"
Just use %date% in your command:
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date%"
Note: this will keep the date in the original format.
Assuming your local date format is Fri 06/07/2013 you can format it into 06072013 by cutting up the string like this:
%date:~4,2%%date:~7,2%%date:~10,4%
So the final command will be:
xcopy /S /E /I %sourceFolder% "C:\Shared\copy\copy-%date:~4,2%%date:~7,2%%date:~10,4%"
Something like this...
for /f "tokens=2-4 delims=/ " %%A in ('echo.%Date%') do set Dest=C:\Shared\copy-%%A%%B%%C
xcopy /S /E /I "%sourceFolder%" "%Dest%"
Related
I made batch that search in folder structure for file mama1.jpg but I need it to ask user for file extension (eg. png, cr2 etc.)
Is it possible?
SET destination=%CD%
for /f "delims=" %%a in ('dir /b /s ^| find "mama.jpg"') do (
if not exist "Skopiowane_zdjecia" md "Skopiowane_zdjecia"
xcopy /y "%%a" "%destination%/Skopiowane_zdjecia")
#echo off
set /p ext="Type file format you search for (eg: jpg, cr2, nef):
SET destination=%CD%
for /f "delims=" %%a in ('dir /b /s ^| find "mama.%ext%"') do (
if not exist "Skopiowane_zdjecia" md "Skopiowane_zdjecia"
xcopy /y "%%a" "%destination%/Skopiowane_zdjecia")
I am writing a batch file to search the registry.
I need to find the folder inside HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products which ProductName key equals to "MyProduct".
I need to find this folder and delete it.
This folder I want to delete will look like this: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\1A0614C849C672CF0A680DCFA3921735
In this example, change MyProduct to your actual ProductName string leaving the closing doublequote untouched, on line 4:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do Echo="%SystemRoot%\System32\reg.exe" Delete "%%G" /F ^>NUL
Pause
The above will only print the deletion line that you intend to run. Once you are satisfied it is correct, to actually delete it, change the code to the following, (remembering to change your ProductName string again). Please note, that as you're deleting a key from HKEY_LOCAL_MACHINE, you will most likely need to run this script elevated, or as a user having sufficient permissions for doing so.:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do "%SystemRoot%\System32\reg.exe" Delete "%%G" /F >NUL
Everyday I create a data log with today's date (eg. ERR150921.txt). I need to copy this file to the server like a backup.
This is my current code:
SET ServerPath=\\IYA-PC\Shared
SET ClientPath=%USERPROFILE%\Documents\Data Log
echo.
echo Copying Files to Server...
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\*.txt" /B /O:F') DO SET NewestFile=%%I
xcopy "%ClientPath%" "%ServerPath%\%NewestFile%" /s /c /d /e /h /i /r /y
I got it from a post here. It copies the recently updated files.
The problem is that I do not need all the updated files. I just need to copy the file created today.
This sample will copy every files that have today date as name before extension. ie it will copy any of this;
"ERR150921.txt"
"21184150921.txt"
"2526150921.txt"
"29056-150921.txt
"150921.txt"
"ABCDEF-150921.txt"
.
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "_timestamp=%YY%%MM%%DD%"
SET "ServerPath=\\IYA-PC\Shared"
SET "ClientPath=%USERPROFILE%\Documents\Data Log"
echo.
echo Copying Files to Server...
setlocal enabledelayedexpansion
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\*.txt" /B') DO (
set "filename=%%~nI"
if "!filename:~-6!"=="%_timestamp%" (
echo xcopy "%%~I" "%ServerPath%\%%~nxI" /s /c /d /e /h /i /r /y
)
)
Get today's date, and transform it to your file name
SET ServerPath=\\IYA-PC\Shared
SET ClientPath=%USERPROFILE%\Documents\Data Log
echo.
echo Copying Files to Server...
SET FILE_NAME=ERR%date:~2,2%%date:~5,2%%date:~8,2%.txt
FOR /F "delims=|" %%I IN ('DIR "%ClientPath%\%FILE_NAME%" /B /O:F') DO SET NewestFile=%%I
xcopy "%ClientPath%" "%ServerPath%\%NewestFile%" /s /c /d /e /h /i /r /y
I want to get the number of files modified before 10 days to a variable.
I can get number of files using
forfiles /P "D:\files" /S /D -10 | find /c /v ""
But when i try to assign it to a variable using FOR it gives error.
Command I used in FOR is
FOR /F "delims=" %i IN ('forfiles /P "D:\files" /S /D -10 | find /c /v ""') DO set today=%i
It actually works fine when I remove | find /c /v ""
FOR /F "delims=" %i IN ('forfiles /P "D:\files" /S /D -10 ^| find /c /v ""') DO set today=%i
in this case you need to escape the pipe.
Yes you can use the FIND command to count how many occurrences it finds but you don't need to. You could just use the set command to iterate a variable.
FOR /F "delims=" %%G IN ('forfiles /P "D:\files" /S /D -10') do #set /a count+=1
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\"