Batch - Error when renaming a file to timestamp - batch-file

I have a program that takes EVERYTHING from the directory
C:\Users\Hensel\Desktop\mc-server\world, and it then it copies and pastes it in a separate directory
C:\Users\Hensel\Desktop\backups\
Then it renames the file called 'world' that it copied and renames it to a date/timestamp format.
#echo off
:backup
xcopy C:\Users\Hensel\Desktop\mc-server\world C:\Users\Hensel\Desktop\backups\world /I /E /Y /D
ren world Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%
ping localhost -n 2700 >nul
goto backup
But whenever I run it (I execute it within the backups directory), it returns the error of: "The syntax of the command is incorrect." Whenever I remove the line that renames 'world' it works fine, but the files won't have unique names when they are backed up unless it has a date and timestamp.

Without knowing your exact date/time format, I'd judge that you have spaces in the new filename.
try
set "newname=Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt"
set newname=%newname: =0%
ren world %newname%
and if that doesn't work (which it should) then tell us what happens with
echo newname="%newname%"
which should be inserted just before the REN
So the full new file should be:
#echo off
:backup
xcopy C:\Users\Hensel\Desktop\mc-server\world C:\Users\Hensel\Desktop\backups\world /I /E /Y /D
set "newname=Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt"
set newname=%newname: =0%
ren world %newname%
echo newname="%newname%"
ping localhost -n 2700 >nul
goto backup
where the echo newname="%newname%" line is optional
Meanwhile, in place of your PING line, you could try
timeout /t 2700 >nul
for a 45-minute delay in an "approved" manner.

Related

How can create a batch script using variables. This makes the file but it wont move the file and delete the source file

Picture of the issuee
#echo off
set date="%date:~4,2%-%date:~7,2%-%date:~10,4%"
if not exist "%USERPROFILE%\Documents\python_scripts\TEST\logs\grab_ip\%date%" mkdir "%USERPROFILE%\Documents\python_scripts\TEST\logs\grab_ip\%date%"
set source="%USERPROFILE%\Documents\python_scripts\TEST\logs\grab_ip"
set target="%USERPROFILE%\Documents\python_scripts\TEST\logs\grab_ip\%date%"
Echo Source is %source%
Echo Target is %target%
Pause
Robocopy.exe %source% %target%
Pause
Here's a 3 line batch-file, which may, (if my assumptions are correct), do what you want:
#Set "source=%USERPROFILE%\Documents\python_scripts\TEST\logs\grab_ip"
#Set "_date=%DATE:~-10%"
#"%__APPDIR__%Robocopy.exe" "%source%" "%source%\%_date:/=-%" /E /Move /XD "%_date:/=-%">NUL
The above is an entire script, do not add or remove any other line or command, before testing. The script assumes your %DATE% value separators are forward slashes, if they are not please modify as necessary.

Use batch file to find a line in files in a folder, delete that line and save the file with the same name

Using a batch file I need to go through all pipe-delimited CSVs in a folder, find the line with dashes and pipes (will always be the second line in the files), delete that line and save the file with the same name in the same location.
The dashes/pipes line will vary in how many dashes are between pipes and will vary on how many pipes there are in the line, so the line can look like this:
------|-----------|---|----------------------|------
or it can look like this:
---------------|--------|-------------------|--|----------|-----|-------
Depends on the data in the file.
I'm just learning looping and findstr in batch files, but I have the following put together through reading and experimenting:
#echo off
set _outputfolder="D:\A Folder\A Sub Folder\Testing Folder"
echo.
echo %_outputfolder%
for %%F in (%_outputfolder%\*.csv) do (
REM Sanity checks
echo.
echo path ^& file: "%%~F"
echo.
echo file: %%~nxF
#findstr /r /b /c:"-*|" "%%~F"
#findstr /r /b /c:"-*|" "%%~F" > "%_outputfolder%"\tmp.csv
copy /y "%_outputfolder%"\tmp.csv "%%F"
)
#pause
The first #findstr correctly echoes the line with dashes in each file, but for each loop iteration for the second #findstr line I get this:
Access is denied.
The syntax of the command is incorrect.
I'm not sure what part of my syntax is incorrect, though. I tried changing the end of the second #findstr line to try to output the file differently, but no matter what I've done so far I keep getting the "Access is denied..." message.
Could you not just do something as crude as this?
#Echo Off
Set "dirSrc=D:\A Folder\A Sub Folder\Testing Folder"
CD /D "%dirSrc%" 2>Nul || Exit /B
For %%A In ("*.csv") Do Findstr "[^-|]" "%%A">"%%~nA.$csv$" && Del /A /F "%%A"
Ren "*.$csv$" "*.csv"

RENAME function not renaming in batch script

EDIT: I have updated this post to include the entire script.
EDIT: While not ideal, this is meant to be an automation of a fix provided by the company that makes the software.
I have a batch file that I am running as Administrator.
I am running a batch file based on the file system input by the user.
One of the first commands renames a file.
If I execute this command on its own, from an elevated command prompt, it renames the file.
When I nest the command inside the IF statement, it doesn't rename the file.
I have commented out all of the other lines to simply rename the file if the user enters "1".
I have tried encapsulating the file path and file each individually within quotes, which I shouldn't need, and still do not get it to rename the file.
I am running the batch script as Admin.
#echo off
setlocal EnableDelayedExpansion
if "%$ecbId%" == "" (
echo Welcome to the ADaPT
echo Choose '1' for 32 bit
echo Choose '2' for 64 bit
echo Type anything else to abort.
echo.
set "UserChoice=abort"
set /P "UserChoice=Type your choice: "
if "!UserChoice!"=="1" (
echo Executing 32 bit sequence...
echo Regsvr32.exe /u C:\Windows\System32\MSCOMCTL.OCX
echo REN C:\Windows\System32\MSCOMCTL.OCX MSCOMCTL.bak
xcopy C:\install\MSCOMCTL.OCX C:\Windows\System32 folder
Regsvr32.exe C:\Windows\System32\MSCOMCTL.OCX
Regsvr32.exe /u C:\Windows\System32\MSCOMCTL.OCX
del "C:\Windows\System32\ MSCOMCTL.OCX"
echo REN "C:\Windows\System32\MSCOMCTL.bak" "MSCOMCTL.OCX"
Recho Regsvr32.exe C:\Windows\System32\MSCOMCTL.OCX
shutdown.exe /r /t 00
)
if "!UserChoice!"=="2" (
echo Regsvr32.exe /u C:\Windows\SYSWOW64\MSCOMCTL.OCX
echo REN "C:\Windows\SYSWOW64\MSCOMCTL.OCX" "MSCOMCTL.bak"
xcopy C:\install\MSCOMCTL.OCX C:\Windows\SYSWOW64 folder
Regsvr32.exe C:\Windows\SYSWOW64\MSCOMCTL.OCX
Regsvr32.exe /u C:\Windows\SYSWOW64\MSCOMCTL.OCX
del "C:\Windows\SYSWOW64\ MSCOMCTL.OCX"
echo REN "C:\Windows\SYSWOW64\MSCOMCTL.bak" "MSCOMCTL.OCX"
echo Regsvr32.exe C:\Windows\SYSWOW64\MSCOMCTL.OCX
REM... shutdown.exe /r /t 00
)
if not "!UserChoice!"=="1" (
echo toto3
if not "!UserChoice!"=="2" (
echo toto4
echo Unknown input ... Aborting script
endlocal
exit /B 400
)
)
)
endlocal
I have simplified your entire script structure so as not to require parenthesised blocks, or the need to delay expansion:
#Echo Off
If Defined $ecbId GoTo :EOF
Echo Welcome to the ADaPT
Choice /C CQ /M "Continue or Quit"
If ErrorLevel 2 GoTo :EOF
Set "Dest=TEM32"
Set PROCESSOR_ARCHITE|Find "64">Nul&&(Set Dest=WOW64)
Set "Dest=%SYSTEMROOT%\SYS%Dest%"
Echo Executing sequence . . .
RegSvr32 /U "%Dest%\MSCOMCTL.OCX"
Rem To backup and replace MSCOMCTL.OCX:
Rem Uncomment the next three unRemarked lines
Rem and comment the next three unRemarked lines below them.
::Del /A /F "MSCOMCTL.bak" 2>Nul
::Ren "%Dest%\MSCOMCTL.OCX" "MSCOMCTL.bak"
::XCopy "C:\install\MSCOMCTL.OCX" "%Dest%"
Rem To recover MSCOMCTL.OCX from the backup:
Rem Comment the next three unRemarked lines
Rem and uncomment the previous three unRemarked lines above them.
If Not Exist "MSCOMCTL.bak" RegSvr32 "%Dest%\MSCOMCTL.OCX" & GoTo :EOF
Del /A /F "%Dest%\MSCOMCTL.OCX"
Ren "%Dest%\MSCOMCTL.bak" "MSCOMCTL.OCX"
RegSvr32 "%Dest%\MSCOMCTL.OCX"
ShutDown /R /T 0 /D P:2:4
Notes:It is probably safe to delete lines 3 and empty line 4, I left them in only because no explanation was provided as to the purpose of the %$ecbId% variable value comparison.I have added Remarks for toggling the script for the backup or recovery methods, but not knowingly altered any of your command sequence, (I have however added the /A and /F options with Del, just in case, and included the /D option with ShutDown to ensure that the logs show it as a planned shutdown event).

How to rename a folder on flash drive using a batch file

I am trying to make a batch file that will back up the contents of a folder onto my flash drive and rename the previous folder on the flash drive. Here is the code.
#echo off
echo Are you sure you want to erase the previous backup file? (Y,N)
set /p ans=
if %ans%==Y goto Backup
goto Exit
:Backup
rd "E:\Batch\BFiles Backup" /s
ren "E:\Batch\BFiles" "BFiles Backup"
:Copy
mkdir E:\Batch\BFiles
xcopy C:\Users\Habib\Documents\BFiles /E /Y E:\Batch\BFiles
echo Files have successfully been copied!
pause
:Exit
exit
When I run the batch file, it copies the files but doesn't rename the already existing folder because "Access is denied". I have tried running it in the administrator version of Cmd, but it still didn't work. My user is an administrator also, so i don't know why access has been denied.
First thing, remove your #ECHO OFF until you are done debugging your code..
Add PAUSE statements so you can stop and see what is going on..
Let's re-format your code a bit..
ECHO Are you sure you want to erase the previous backup file? (Y,N)
set /p ans=
if %ans%==Y goto Backup
PAUSE
goto Exit
:Backup
PAUSE
IF EXIST "E:\Batch\BFiles Backup\." rd "E:\Batch\BFiles Backup" /s
PAUSE
IF NOT EXIST "E:\Batch\BFiles Backup\." ren "E:\Batch\BFiles" "BFiles Backup"
PAUSE
:Copy
IF NOT EXIST "E:\Batch\BFiles\." mkdir E:\Batch\BFiles
PAUSE
xcopy C:\Users\Habib\Documents\BFiles /E /Y E:\Batch\BFiles
IF errorlevel 0 echo Files have successfully been copied!
pause
:Exit
exit
Then, when things start looking correct, remove the PAUSE statements one by one...
Hope this helps!

Delete files in subfolder using batch script

I have to delete files from a sub folder with a same name. My file path is like as follows.
d:\test\test1\archive\*.txt
d:\test\try\archive\*.txt
d:\test\model\archive\*.txt
I tried deleting using del command in batch script.
But there are more than 100 folders with in the folder "test". So it is very difficult to use del for each and every path.
Except for the parent folder name of "archive" folder, everything remains the same for all the paths. So I guess there might be some easy way to delete the files using batch script.
Can anyone guide me whether there is any easy way to delete the files using batch script? Or i have to repeat del for all 100 folders?
You can use the /s switch for del to delete in subfolders as well.
Example
del D:\test\*.* /s
Would delete all files under test including all files in all subfolders.
To remove folders use rd, same switch applies.
rd D:\test\folder /s /q
rd doesn't support wildcards * though so if you want to recursively delete all subfolders under the test directory you can use a for loop.
for /r /d D:\test %a in (*) do rd %a /s /q
If you are using the for option in a batch file remember to use 2 %'s instead of 1.
Use powershell inside your bat file
PowerShell Remove-Item c:\scripts\* -include *.txt -exclude *test* -force -recurse
You can also exclude from removing some specific folder or file:
PowerShell Remove-Item C:/* -Exclude WINDOWS,autoexec.bat -force -recurse
Moved from the closed topic
del /s d:\test\archive*.txt
This should get you all of your text files
Alternatively,
I modified a script I already wrote to look for certain files to move them, this one should go and find files and delete them. It allows you to just choose to which folder by a selection screen.
Please test this on your system before using it though.
#echo off
Title DeleteFilesInSubfolderList
color 0A
SETLOCAL ENABLEDELAYEDEXPANSION
REM ---------------------------
REM *** EDIT VARIABLES BELOW ***
REM ---------------------------
set targetFolder=
REM targetFolder is the location you want to delete from
REM ---------------------------
REM *** DO NOT EDIT BELOW ***
REM ---------------------------
IF NOT DEFINED targetFolder echo.Please type in the full BASE Symform Offline Folder (I.E. U:\targetFolder)
IF NOT DEFINED targetFolder set /p targetFolder=:
cls
echo.Listing folders for: %targetFolder%\^*
echo.-------------------------------
set Index=1
for /d %%D in (%targetFolder%\*) do (
set "Subfolders[!Index!]=%%D"
set /a Index+=1
)
set /a UBound=Index-1
for /l %%i in (1,1,%UBound%) do echo. %%i. !Subfolders[%%i]!
:choiceloop
echo.-------------------------------
set /p Choice=Search for ERRORS in:
if "%Choice%"=="" goto chioceloop
if %Choice% LSS 1 goto choiceloop
if %Choice% GTR %UBound% goto choiceloop
set Subfolder=!Subfolders[%Choice%]!
goto start
:start
TITLE Delete Text Files - %Subfolder%
IF NOT EXIST %ERRPATH% goto notExist
IF EXIST %ERRPATH% echo.%ERRPATH% Exists - Beginning to test-delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
echo "%%a" "%Subfolder%\%%~nxa"
)
popd
echo.
echo.
verIFy >nul
echo.Execute^?
choice /C:YNX /N /M "(Y)Yes or (N)No:"
IF '%ERRORLEVEL%'=='1' set question1=Y
IF '%ERRORLEVEL%'=='2' set question1=N
IF /I '%question1%'=='Y' goto execute
IF /I '%question1%'=='N' goto end
:execute
echo.%ERRPATH% Exists - Beginning to delete files...
echo.Searching for .txt files...
pushd %ERRPATH%
for /r %%a in (*.txt) do (
del "%%a" "%Subfolder%\%%~nxa"
)
popd
goto end
:end
echo.
echo.
echo.Finished deleting files from %subfolder%
pause
goto choiceloop
ENDLOCAL
exit
REM Created by Trevor Giannetti
REM An unpublished work
REM (October 2012)
If you change the
set targetFolder=
to the folder you want you won't get prompted for the folder.
*Remember when putting the base path in, the format does not include a '\' on the end.
e.g.
d:\test
c:\temp
Hope this helps
I had to complete the same task and I used a "for" loop and a "del" command as follows:
#ECHO OFF
set dir=%cd%
FOR /d /r %dir% %%x in (archive\) do (
if exist "%%x" del %%x\*.txt /f /q
)
You can set the dir variable with any start directory you want or used the current directory (%cd%) variable.
These are the options for "for" command:
/d For directories
/r For recursive
These are the options for "del" command:
/f Force deletes read-only files
/q Quiet mode; suppresses prompts for delete confirmations.
del parentpath (or just place the .bat file inside parent folder) *.txt /s
That will delete all .txt files in the parent and all sub folders. If you want to delete multiple file extensions just add a space and do the same thing. Ex. *.txt *.dll *.xml

Resources