Task Scheduler response (0x1) when copy file to map disk - windows

I write code to backup file to Map disk. It worked when I run it manual (created zip file and copy file to map disk).
Here is my code:
#echo off
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=web2_%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
md C:\Backup\"%SUBFILENAME%"
md C:\Backup\"%SUBFILENAME%"\05_SOHOA_HOSOLUUTRU
XCOPY "E:\Data_SoHoa\05_SOHOA_HOSOLUUTRU" "C:\Backup\%SUBFILENAME%\05_SOHOA_HOSOLUUTRU" /E
md C:\Backup\"%SUBFILENAME%"\05_SOHOA_HOSOLUUTRU_API
XCOPY "E:\Data_SoHoa\05_SOHOA_HOSOLUUTRU_API" "C:\Backup\%SUBFILENAME%\05_SOHOA_HOSOLUUTRU_API" /E
md C:\Backup\"%SUBFILENAME%"\DATA
XCOPY "E:\Data_SoHoa\DATA" "C:\Backup\%SUBFILENAME%\DATA" /E
XCOPY "E:\Data_SoHoa\setup-TcpServerService.exe" "C:\Backup\%SUBFILENAME%\" /X
set PATH=C:\Program Files\7-Zip\
SETX MYPATH "%PATH%"
7z a C:\"%SUBFILENAME%".zip C:\Backup\"%SUBFILENAME%"
set copyfile = C:\%SUBFILENAME%.zip
COPY "C:\%SUBFILENAME%.zip" "Z:\Backup\TLAM_sohoa\%SUBFILENAME%.zip"
Then I setup Task Scheduler to auto run this bat script. It created zip file, but not copy this zip file to map disk and return (0x1) result.
Here is my setup

I have decided to post this as a rewritten example. It formulates the date string in a none locale/PC dependent manner, reduces repetetive and unnecessary commands, replaces the deprecated XCopy with RoboCopy and uses PushD to create a temporary mapping to your server location for the final copy command.
#Echo Off
Set "SubFileName="
For /F "Tokens=1-6Delims=/: " %%A In ('RoboCopy/NJH /L "\|" Null'
)Do If Not Defined SubFileName Set "SubFileName=web2_%%A%%B%%C-%%D%%E%%F"
Set "CopyFile=C:\%SubFileName%.zip"
For %%A In ("05_SOHOA_HOSOLUUTRU" "05_SOHOA_HOSOLUUTRU_API" "DATA"
)Do RoboCopy "E:\Data_SoHoa\%%~A" "C:\Backup\%SubFileName%\%%~A" /E
RoboCopy "E:\Data_SoHoa" "C:\Backup\%SubFileName%" "setup-TcpServerService.exe" /CopyAll
"%ProgramFiles%\7-Zip\7z.exe" a "%CopyFile%" "C:\Backup\%SubFileName%"
PushD "\\server…\Backup\TLAM_sohoa"
Copy "%CopyFile%"
PopD
Just modify server… on line 10 to the UNC path of the location, which after logon would be, associated with Z:

Related

Using a batch script, copy only those files that match a certain string in file name

I am trying to find a way to find, then copy to a new folder, files that are marked with a certain date stamp.
My folder structure is like this:
D:\GOES DATA CENTER\2021-12-31\ D:\GOES DATA CENTER\2022-01-01
D:\GOES DATA CENTER\2022-01-02\ D:\GOES DATA CENTER\2022-01-03\
Within each of those folders are files such as this
GOES16_FD_FC_CUSTOMLUT_20220101T140020Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T143020Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T150019Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T153020Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T160020Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T160018Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T170020Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T173019Z.jpg
GOES16_FD_FC_CUSTOMLUT_20220101T180020Z.jpg etcetera
What I need is a command line(s) for a batch script to find each file starting on a certain date, with only the "T1700" timestamp, then copy that file to a new folder.
this is what I have tried:
for /R "D:\GOES DATA CENTER\goes16\fd\CUSTOMLUT\" %%f in (*.jpg) do xcopy "%%f" "F:\SatelliteInput\CUSTOMLUT\compositeFC" /D /D:%startdate%
This solution has worked for what I need; using Variables for %satellite%, %bandtype% and %startdate% - I'll add a user defined variable for the time as well later.
for /R "D:\GOES DATA CENTER\goes%satellite%\fd\%bandtype%\" %%f in (*T1700*.jpg) do xcopy "%%f" "D:\TEMPPROCESSING\HIGHNOON" /D /D:%startdate% /S /C /Y
To iterate each folder within the root folder:
#echo off
for /R "D:\GOES DATA CENTER" %%i in ("*T1700???.jpg") do copy "%%~i" "D:\destination folder\"
or to do only folders you define in a list:
#echo off
set "dirs="D:\GOES DATA CENTER\2021-12-31" "D:\GOES DATA CENTER\2022-01-01" "D:\GOES DATA CENTER\2022-01-02" "D:\GOES DATA CENTER\2022-01-03""
for %%i in (%dirs%) do copy "%%~i\*T1700???.jpg" "destination folder"

Change Directory of Batch File to the batch file's location

Weekly, I have to update our computers with publications for our aircraft. I have streamlined the process by creating a batch file that deletes the old publications automatically and opens the folder where they are stored so I don't have to dig for it. The location where the publications are stored each week is the same. My batch file looks like below...
del /s /f "C:\Users\Public\Desktop\PubFolder\Pub.pdf"
So basically all I have to do is copy the PubFolder from my USB drive to the Public Desktop after I have ran the batch file to delete all of the publications.
If all of our computers were exactly the same, this would be an easy fix because I could write in the batch file
xcopy /y D:\PubFolder" "C:\Users\Public\Desktop\PubFolder"
and the script would do all of the work for me. My issue is, a lot of the computers have different software installed on them which require partitions in the HDD. Some of them have 3, some 2, some 1.
Basically what I'm needing is when I run the batch file from the USB, it uses the location of the batch file as the directory so I can copy from there.
A cmd script (.bat file) can reference its own directory with %~dp0
See the details using FOR /?
While you are at it, some additional error checking might make some situations easier to recover from.
SETLOCAL
SET "EXITCODE=0"
SET "USERDIR=%PUBLIC%\Desktop\PubFolder"
IF NOT EXIST "%USERDIR%" (
ECHO ERROR: The user publication directory "%USERDIR%" does not exist.
SET EXITCODE=4
GOTO TheEnd
)
IF EXIST "%USERDIR%\Pub.pdf" (DEL "%USERDIR%\Pub.pdf")
xcopy /y %~dp0PubFolder" "%USERDIR%"
SET "EXITCODE=%ERRORLEVEL%"
IF %EXITCODE% NEQ 0 (
ECHO ERROR: Failed to copy from "%~dp0" to "%USERDIR%"
SET EXITCODE=5
GOTO TheEnd
)
:TheEnd
EXIT /B %EXITCODE%
#setlocal
REM If you have pre-Vista machines, uncomment the folowing line:
REM #if not defined PUBLIC set PUBLIC=%SYSTEMDRIVE%\Users\Public%
#xcopy /y %~d0\PubFolder %PUBLIC%\Desktop\PubFolder

How to execute an application existing in each specific folder of a directory tree on a file in same folder?

I have some folders with different names. Each folder has a specific structure as listed below:
Folder1
Contents
x64
Folder1.aaxplugin
TransVST_Fixer.exe
Folder 2
Contents
x64
Folder 2.aaxplugin
TransVST_Fixer.exe
There are two files within each subfolder x64. One file has the same name as the folder two folder levels above. The other file is an .exe file whose name is the same in all folders.
Now I need to run file with file extension aaxplugin on each specific .exe file. It would be obviously very time consuming opening each and every single folder and drag & drop each file on .exe to run it on this file.
That's why I am trying to create a batch script to save some time.
I looked for solutions here on Stack Overflow. The only thing I have found so far was a user saying this: When I perform a drag & drop, the process 'fileprocessor.exe' is executed. When I try to launch this exe, though, CMD returns error ('not recognized or not batch file' stuff).
How can I do this?
UPDATE 12/22/2015
I used first a batch file with following line to copy the executable into x64 subfolder of Folder1.
for /d %%a in ("C:\Users\Davide\Desktop\test\Folder1\*") do ( copy "C:\Program Files\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%a\x64\" 2> nul )
After asking here, I tried the following script:
for /f "delims=" %%F in ('dir /b /s x64\*.aaxplugin') do "%%~dpFTransVST_Fixer.exe" "%%F"
Unfortunately, the output is as following
C:\Users\Davide\Desktop>for /F "delims=" %F in ('dir /b /s x64\*.aaxplugin') do "%~dpFTransVST_Fixer.exe" "%F"
The system cannot find the file specified.
Try the following batch code:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\Desktop\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
The command FOR with option/R searches recursive in all directories of directory %USERPROFILE%\Desktop\test being expanded on your machine to C:\Users\Davide\Desktop for files with file extension aaxplugin. The loop variable F contains on each loop run the name of the found file with full path without surrounding double quotes.
The drive and path of each found file is assigned to environment variable FilePath.
Next a case-sensitive string comparison is done between file path with all occurrences of string \x64\ case-insensitive removed with unmodified file path.
Referencing value of environment variable FilePath must be done here using delayed expansion because being defined and evaluated within a block defined with ( ... ). Otherwise command processor would expand %FilePath% already on parsing the entire block resulting in a syntax error on execution because string substitution is not possible as no environment variable FilePath defined above body block of FOR loop.
The strings are not equal if path of file contains a folder with name x64. This means on provided folder structure that the file is in folder x64 and not somewhere else and therefore the application is executed next from its original location to fix the found *.aaxplugin file.
The line with IF is for the folder structure example:
if not "C:\Users\Davide\Desktop\test\Folder1\Contents" == "C:\Users\Davide\Desktop\test\Folder1\Contents\x64\"
if not "C:\Users\Davide\Desktop\test\Folder 2\Contents" == "C:\Users\Davide\Desktop\test\Folder 2\Contents\x64\"
So for both *.aaxplugin files the condition is true because the compared strings are not identical
Also possible would be:
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%F in ('dir /A-D /B /S "%USERPROFILE%\test\*.aaxplugin" 2^>nul') do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
But command DIR is not really necessary as it can be seen on first provided code.
But if the application TransVST_Fixer.exe for some unknown reason does its job right only with directory of file being also the current directory, the following batch code could be used instead of first code using the commands pushd and popd:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
echo !FilePath!
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dpF"
"%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%~nxF"
popd
)
)
endlocal
There is one more difference in comparison to first code. Now the last 5 characters of path of file are compared case-insensitive with the string \x64\. Therefore the file must be really inside a folder with name x64 or X64. A folder with name x64 or X64 anywhere else in path of file does not result anymore in a true state for the condition as in first two batch codes.
But if for some unknown reason it is really necessary to run the application in same folder as the found *.aaxplugin and the directory of the file must be the current directory, the following batch code could be used:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%# in (*.aaxplugin) do (
set "FilePath=%%~dp#"
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dp#"
"%%~dp#TransVST_Fixer.exe" "%%~nx#"
popd
)
)
endlocal
The path of the file referenced with %%~dpF always ends with a backslash which is the reason why there is no backslash left of TransVST_Fixer.exe (although command processor could handle also file with with two backslashes in path).
In batch code above character # is used as loop variable because %%~dp#TransVST_Fixer.exe is easier to read in comparison to %%~dpFTransVST_Fixer.exe. It is more clear for a human with using # as loop variable where the reference to loop variable ends and where name of application begins. For the command processor it would not make a difference if loop variable is # or upper case F.
A lower case f would work here also as loop variable, but is in general problematic as explained on Modify variable within loop of batch script.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
if /?
popd /?
pushd /?
set /?
setlocal /?
Your question isn't quite clear, but it seems, something like this should work:
for /f "delims=" %%f in ('dir /b /s X64\*.ext') do "%%~dpfMyExe.exe" "%%f"
Maybe you have to change directory to each folder (depends on your .exe):
for /f "delims=" %%d in ('dir /B /ad') do (
pushd "%%d"
for /f "delims=" %%f in ('dir /b "contents\x64\*.ext"') do (
cd Contents\x64
MyExe.exe "%%f"
)
popd
)
Assuming:
The Directory structure is fixed and the files are indeed in a subfolder contents\X64\.
MyExe.exe is the same (name) in every folder.
There is only one file *.ext in every folder.
I'll give you the script I created for doing so, hope it works for you
for /d %%d IN (./*) do (cd "%%d/Contents/x64" & "../../../TransVST_Fixer.exe" "%%d" & cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins")
Please note that I placed the fixer inside the root folder so I just have to copy it once. You have to place it inside your root folder and execute it. What it does:
iterate over each folder
for each one it enters /Contents/x64, executes the fixer (wich is 3 levels above) and after that returns to the original folder.
If you have your plugins in a different folder, you just have to change this part replacing the path for the one you have your plugins in.
cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins"
REMEMBER to place the script on that folder. For this example I place my script on the folder "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins" and run it (as a .bat).
PS: the fixer will place the fixed plugins in "C:\Users\Public\modified" (just read the screen while executing, it gives you the new files path. If you want to move them to the right path, you can execute this from the new files path ("C:\Users\Public\modified")
for %%d IN (*.aaxplugin) do (mkdir "%%d_temp/Contents\x64" & move "%%d" "%%d_temp/Contents\x64/%%d" & rename "%%d_temp" "%%d")
with that, I iterate over every plugin and create a folder with the same name (I create _temp because of name colision, after moving the file I rename it to the correct one), also with the subfolder "/Contents/x64", and move the plugin inside. Once donde, you can just take the resulting folders and place them in their correct path.
Hope it works, for me it works like a charm.

Drag and drop to batch file from UNC - how to capture %cd%

I have a batch file:
#ECHO OFF
Set dd=%DATE:~0,2%
Set mm=%DATE:~3,2%
Set yyyy=%DATE:~6,4%
Set hh=%TIME:~0,2%
Set ii=%TIME:~3,2%
Set ss=%TIME:~6,2%
Set zipFileHandle=%yyyy%-%mm%-%dd%-%hh%-%ii%-%ss%
Set files=%*
%~dp0\7za a -t7z %cd%\%zipFileHandle%.7z %files%
When I drop a group of files and/or directories on it, it compresses them into a dated .7z file in the root folder they all came from.
The problem is that if I drop network files, with a path starting with \\, the batch file changes the value of the save directory to C:\Windows.
How can I get the value of %cd% before cmd changes it to the system root?
If that's not possible, is it possible to get the common root folder from the variable %files%?
You should get next message:
'\\computer\path'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
So you could use next:
%~dp07za a -t7z %~dp1%zipFileHandle%.7z %files%
Note that \ backslash could be omitted using %~dp0 and %~dp1 as the ~dp modifier expands a %variable to a Path only including a trailing \ backslash.
And if source folder name contains spaces, use quoted target file name rather:
%~dp07za a -t7z "%~dp1%zipFileHandle%.7z" %files%
You could pushd %~dp1 before calling 7za. That temporarily maps the UNC path of the 1st dragged-and-dropped file as a network drive letter and changes directory to it. The mapping disappears as soon as the script exists.
Additionally, 7za has exit codes you might make use of for fault tolerance.
#echo off
setlocal
for /f "tokens=2 delims=.=" %%I in (
'wmic os get localdatetime /format:list ^| find "="'
) do set "t=%%I"
set "handle=%t:~0,4%-%t:~4,2%-%t:~6,2%_%t:~8,2%-%t:~10,2%-%t:~12,2%"
pushd "%~dp1"
"%~dp0\7za" a -t7z "%handle%.7z" %* || (
if ERRORLEVEL 2 (
echo Zipping failed.
pause
) else (
echo Zipping completed with errors, possibly because a file is locked by another process.
pause
)
)

Batch file to move many listed files to one specific folder

I have a Master folder which have multiple sub folders. All the subfolders have lot images with different extensions (jpg,tif and png). The total number of images in all the subfolder are around 90000 images.
The thing is, I need to search some 500 images in Master folder and its subfolders and move the images to specified folder.
I tried a below batch script to use the text file to search through a Master folder and all subfolders and move all the files from the list and paste them into a Specified single folder.
The text file which contains file names without extension.
But my batch script is not working. It didnt throw me any error.. but nothing happens when I run it.
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f %%X in (%FIILELIST%) do call :MOVE_FILES "%%X"
goto :eof
:MOVE_FILES
for /r %FILESPATH% %%I in (%~1) do echo move /qvs "%%I" "%DESTPATH%%%~pnxI"
I am very new to batch script and in learning stage. Kindly help me in this. Im very much thankful if anyone provide the correct batch script to do this.
Can you try this?
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %FILESPATH% /s /m %%x.* /c "cmd /c move /y #path %DESTPATH%\#file" 2>>failed_temp.txt)
for /f "tokens=5 " %i in (failed_temp.txt) do (echo.%~i)>>failed_list.txt
del failed_temp.txt
Cheers, G
#gbabu's answer was super helpful for me.
I needed to edit it to handle file names that were absolute (full) instead of relative. And I needed to handle that they contained spaces.
Unfortunately I couldn't figure out how to log the errors like #gbabu did.
#echo off
REM See https://stackoverflow.com/a/25325529/470749
REM See https://stackoverflow.com/a/163873/470749
REM See https://stackoverflow.com/a/155950/470749
set FILELIST="K:\F\Users\my_user\Documents\My Music\JUKEBOX\5.m3u_list.txt"
set DESTPATH=C:\temp\cdrw
for /f "usebackq tokens=*" %%x in (%FILELIST%) do (copy "%%x" %DESTPATH%)
pause
These articles helped me:
https://stackoverflow.com/a/25325529/470749
https://stackoverflow.com/a/163873/470749
https://stackoverflow.com/a/155950/470749

Resources