If i play this batch file, it send me error message such like this,
"the system cannot find the file specified"
I searched a lot, but i do not know ...
i saw this article already, but it did not work.
System cannot find the path specified
echo off
echo **** TEST BACK UP BATCH FILE STARTS ****
setlocal enabledelayedexpansion
:LOOP
FOR %%f in (C:\Web\FTP\A\*.txt) do (
xcopy /Y %%f "C:\Web\FTP\A\B\"
SET "file=%%~nf"
SET "mydate=!date:~10,4!!date:~7,2!!date:~4,2!"
SET "mytime=!time:~0,2!!time:~3,2!!time:~6,2!"
ren "C:\Web\FTP\A\B\%file%.txt" "!file!_!mydate!!mytime!.txt"
move /Y "C:\Web\FTP\A\!file!.txt" "C:\Web\FTP\A\C\in\"
)
timeout /t 15
goto LOOP
Here are all the changes suggested in the comments above.
echo off
setlocal enabledelayedexpansion
echo **** TEST BACK UP BATCH FILE STARTS ****
:LOOP
FOR %%G in (C:\Web\FTP\A\*.txt) do (
SET "mydate=%date:~10,4%%date:~7,2%%date:~4,2%"
SET "mytime=%time:~0,2%%time:~3,2%%time:~6,2%"
xcopy /Y "%%~G" "C:\Web\FTP\A\B\%%~nG_!mydate!!mytime!.txt"
move /Y "%%~G" "C:\Web\FTP\A\C\in\"
)
timeout /t 15
goto LOOP
Related
I have some PDF files that I need to rename and move to specific folders to be able to import into a system.
The file by default has the following name:
ex: 12345.123456/1234-12.pdf
First I need to remove the characters "," "/" "-"
#echo off
setlocal EnabledelayedExpansion
for /r "C:\importation" %%a in (*) do (
set "newname=%%~na"
set "newname=!newname:.=!"
set "newname=!newname:-=!"
set "newname=!newname:/=!"
ren "%%~a" "!newname!%%~xa"
)
Now I need to create folders with the filename (without the characters removed, ex: 12345123456123412) and rename them to the following pattern:
ex: P12345123456123412_V1_A0V0_T07-54-369-664_S00001_Volume.pdf
For that I drag the files to the following script:
#If Not "%~1" == "" For %%G In (%*) Do #MD "%%~dpG%%~nG" 2>NUL && Move /Y "%%~G" "%%~dpG%%~nG\P%%~nG_V1_A0V0_T07-54-369-664_S00001_Volume%%~xG"
I would like to do only one process, that is, join the two scripts and run only once.
Can someone help me?
Rather than dragging the files to the batch file I would like to run it and have it read the files (.pdf) from the folder
Is something like this what you are looking for?
The following will accept drag and drop of one or more files or directories, (subject to command line length restrictions). Each individually dropped PDF file and each PDF file within any dropped directory should be processed as requested.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
If "%~1" == "" GoTo :EOF
For %%G In (%*) Do (For %%H In ("%%~G") Do If "%%~aH" Lss "-" (
Echo Error! %%G no longer exists.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
) Else If "%%~aH" GEq "d" (For %%I In ("%%~G\*.pdf") Do Call :Sub "%%~I"
) Else If /I "%%~xG" == ".pdf" (Call :Sub "%%~G"
) Else (Echo Error! %%G is not a PDF
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL))
GoTo :EOF
:Sub
Set "basename=%~n1"
Set "basename=%basename:.=%"
MD "%~dp1%~n1" 2>NUL
If Not ErrorLevel 1 Move /Y %1 "%~dp1%~n1\P%basename:-=%_V1_A0V0_T07-54-369-664_S00001_Volume%~x1"
Exit /B
Please note, as the character / is invalid in file or directory names, I have ignored that part of your question.
I have some pdf's in a folder that I need to organize them like this:
PDF name: 123.12.123.pdf ; 102030_01.pdf; 102030_02.pdf; 123.4512.34561.23412.pdf
Now I need to create folders with the filename (without the characters removed, ex: 12345123456123412) and rename them to the following pattern: ex: P12345123456123412_V1_A0V0_T07-54-369-664_S00001.pdf
for this I have used the following code which works very well:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
If "%~1" == "" GoTo :EOF
For %%G In (%*) Do (For %%H In ("%%~G") Do If "%%~aH" Lss "-" (
Echo Error! %%G no longer exists.
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
) Else If "%%~aH" GEq "d" (For %%I In ("%%~G\*.pdf") Do Call :Sub "%%~I"
) Else If /I "%%~xG" == ".pdf" (Call :Sub "%%~G"
) Else (Echo Error! %%G is not a PDF
%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL))
GoTo :EOF
:Sub
Set "basename=%~n1"
Set "basename=%basename:.=%"
MD "%~dp1%~n1" 2>NUL
If Not ErrorLevel 1 Move /Y %1 "%~dp1%~n1\P%basename:-=%_V1_A0V0_T07-54-369-664_S00001_Volume%~x1"
Exit /B
I drag the pdfs into the .bat and it does the adjustment.
It happens that there is a case that I am not able to handle. Some pdfs need to be in the same folder, for example in the following case:
PDF name: 102030_01.pdf; 102030_02.pdf;
Note that the pdfs have the same number, only after the _ that we have the difference. In this case you would need to create a folder with the name:102030
And move the two files into it, modifying their name as follows:
102030_01.pdf -> P102030_V1_A0V0_T07-54-369-664_S00001.pdf
102030_02.pdf -> P102030_V1_A0V0_T07-54-369-664_S00002.pdf
Could anyone help?
:Sub
Set "basename=%~n1"
Set "basename=%basename:.=%"
if /i "%basename%" neq "%basename:_=%" goto sub2
MD "%~dp1%~n1" 2>NUL
If Not ErrorLevel 1 Move /Y %1 "%~dp1%~n1\P%basename:-=%_V1_A0V0_T07-54-369-664_S00001_Volume%~x1"
Exit /B
:sub2
for /f "tokens=1*delims=_" %%b in ("%basename%") do (
MD "%~dp1%%b" 2>NUL
ECHO Move /Y %1 "%~dp1%%b\P%basename:-=%_V1_A0V0_T07-54-369-664_S000%%c_Volume%~x1"
)
Exit /B
Always test on dummy data first.
This code echoes the proposed move. After verification, remove the echo keyword to activate.
Caution: My reading of the code is that - should be removed from the basename in the new name, and that _Volume should be appended to the name part, which is not shown in your examples.
Essentially, if the basename contains _ then goto sub2.
sub2 partitions the name in basename, assigning the first part to %%b and the second to %%c (See for /? from the prompt for documentation)
Then the directory is created
The md will object if the directory already exists, hence the 2>nul in the original code (suppresses error messages)
If md found that error in the original then this appears to be a problem, so the move is not executed. In the new version, it is expected that the directory may already exist, so the errorlevel processing has been removed.
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).
hi guys i'm a trying to move text files one by one with a timeout of 8 seconds from one folder to another using a batch script. i have this script so far;
move /-y "D:\example\original\*2007*.txt" "D:\example\New folder\"
what should i add so that it doesn't move the files at once?
As #npocmaka recommend to you the user confirmation for example like this batch script : Movies wrapper script
#ECHO OFF
SETLOCAL
SET "ROOT=%~dp0"
SET "FORCE=0"
IF /I "%~1"=="/f" (
SET "FORCE=1"
SHIFT
)
IF "%~1"=="" GOTO :END_PARSE
SET "ROOT=%ROOT%%~1\"
:END_PARSE
ECHO -----------------------------------------------
ECHO WRAPPER - "%ROOT%"
ECHO -----------------------------------------------
SET "COUNT_SUCC=0"
SET "COUNT_FAIL=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
FOR %%f IN ("%ROOT%\*.*") DO (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
IF !FORCE! NEQ 1 (
rem echo force=!Force!
SET /P "INPUT=Do you want to wrap the file "%%~nxf" ? (Y/[N])"
IF /I "!INPUT!"=="Y" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\">NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\">NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
) Else (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
)
)
)
ECHO.
SET /a "COUNT_TOT=COUNT_SUCC+COUNT_FAIL"
ECHO Total of %COUNT_TOT% files(s) : %COUNT_SUCC% file(s) wrapped and %COUNT_FAIL% file(s) failed.
ECHO.
PAUSE
use a simple for loop to process each file individually:
for %%a in ("D:\example\original\*2007*.txt") do (
move /-y "%%~fa" "D:\example\New folder\"
timeout /t 8
)
For more information, see for /?
Is there a way to check if a new folder (exact name is unknown) has been created inside a specific parent folder as a result of running previous commands in bat file? If yes - run one more command with the full path to a new folder as an argument to that command.
Right now I am using a folder monitoring software to run another bat if new folder is created. I would like to have just one script performing both tasks.
Thank you for your help.
change the root_folder location on the second line
#echo off
set "root_folder=C:\something"
setlocal enableDelayedExpansion
set counter=1
for /d /r "%root_folder%" %%a in (*) do (
set "dirs[!counter!]=%%~sa" >nul
set /a counter=counter+1
)
rem ############################
rem # call your code here !!! #
rem ############################
call commands.bat
rem for /l %%l in (1,1,!counter!) do (
rem dir /x /b /s /a:d "%root_folder%" | findstr /i "dirs[%%l]"
rem )
set flag=0
for /d /r "%root_folder%" %%a in (*) do (
set dirs[|find /i "%%~sa" >nul 2>nul ||(
echo "%%~sa" is a new folder
set flag=1
)
)
if %flag% equ 0 (
echo no new folders
)