I am trying to add additional functionality to an existing batch file, the below snippet of code copies my data from one location to another and then deletes the zip file that was created, all works well.
what I want to do is if the file already exists on the USB drive to add an incremental number to the end of the file eg. FileName-FileDate(1) or FileName-FileDate(2) I could add time to the FileStamp which at the moment adds the date but this could make the file name a bit long.
I am copying the file to a USB drive as a backup, this is why I am using drives E, F, G, H, dependant in what Windows sees the drive as, it is also important that the created file on the C: drive is deleted due to space.
I am using the /y to reduce user input as it will be done by people with limited technology skills so I don't really want to use /-y. also the machine does not have a keyboard, only a touch screen, so renaming the file is not an easy thing to do.
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Copy the backup file to a USB drive with File Name and Date Stamp ------
IF EXIST E: (echo copying files to USB drive E:
copy %BackupPath%\Backup\%FileStamp%.zip E: /y )
IF EXIST F: (echo copying files to USB drive F:
copy %BackupPath%\Backup\%FileStamp%.zip F: /y )
IF EXIST G: (echo copying files to USB drive G:
copy %BackupPath%\Backup\%FileStamp%.zip G: /y )
IF EXIST H: (echo copying files to USB drive H:
copy %BackupPath%\Backup\%FileStamp%.zip H: /y )
REM ------ Delete the temporary zip file from the backup folder ------
echo Deleting temporary zip file from the backup folder
Del %BackupPath%\Backup\%FileStamp%.zip
IF EXIST does not work on drive letters and directories, but fortunately, there is an obscure workaround:
C:\> IF NOT EXIST D:\NUL ECHO D:\ not present.
D:\ not present.
C:\> IF EXIST C:\NUL ECHO C:\ exists.
C:\ exists.
It turns out that to support constructs like appending >NUL on command statements, there is a sort of virtual file named "NUL" in every directory including the root directory of a drive. Checking for its existence is equivalent to a check for the directory or drive's existence.
This behavior is documented in a Microsoft knowledge base article ( https://support.microsoft.com/en-us/kb/65994 ) and I have confirmed its behavior on FreeDOS 1.1 and in a Windows 7 command shell.
Though the KB article addresses checking for directory existance, it also indicates the technique can be used to see if a drive is present. In the case of checking for drive existence, however, caveats exist:
An Abort, Retry, Fail? error occurs if the drive is not formatted.
Using this technique to check for drive existence depends on device driver implementation and may not always work.
Related
Have 2 drives:
network drive, where reports are located
Local disk, where 7zip is located.
Also, we have many users, everybody mapped network drive with different letter. Hence, the path to report will not be the same for everyone.
Script and reports located in the same folder.
Try to create script.bat to archive reports on network drive. Here is what I have:
#echo off
::set a path to network shared directory
pushd \\IP_address\path_to_reports
set BEpath= %cd%
:: Move files to temporary directory FilesToZip
mkdir "%BEpath%\FilesToZip"
move %BEpath%\report_Germany_??_%1_??.txt %BEpath%\FilesToZip\
:: Archive files
cd /d "C:\Program Files\7-Zip"
7z a -t7z "%BEpath%\archive.7z" "%BEpath%\FilesToZip\*.txt"
If I use just CD \\server\path instead of pushd \\IP_address\path_to_reports it gives error
CMD does not support UNC paths as current directories.
Looks like it can not zip on network drive from C:\ drive and gives error:
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21
Scanning the drive:
WARNING: The filename, directory name, or volume label syntax is incorrect.
Z:
0 files, 0 bytes
Creating archive: Z:\path\archive.7z
Add new data to archive: 0 files, 0 bytes
Scan WARNINGS for files and folders:
Z: : The filename, directory name, or volume label syntax is incorrect.
----------------
Scan WARNINGS: 1
Error:
cannot open file
Z:\path\archive.7z
The filename, directory name, or volume label syntax is incorrect.
Could you advice, how it can be done?
Thank you
7z supports UNC path natively.
Instead of trying to use the temp drive created by pushd:
7z a -t7z "%BEpath%\archive.7z" "%BEpath%\FilesToZip\*.txt"
Use the UNC path directly:
set "UNCpath=\\IP_address\path_to_reports"
7z a -t7z "%UNCpath%\archive.7z" "%UNCpath%\FilesToZip\*.txt"
You have a couple options.
1) You can parse the output of wmic logicaldisk get caption,drivetype,providername for drivetype=4 (network drive) where providername='\IP_address\path_to_reports'. Then caption will have their mapped drive letter for your use directly. Be aware that WMIC is deprecated so this approach will someday stop working for you.
2) You can just remap it yourself to an unlikely letter. I find that almost nobody uses A:.
net use a: \\IP_address\path_to_reports
set BEpath=a:\
...do stuff here...
net use a: /delete
I created a .bat file to replace 3 files from a DVD burned into a directory on my PC, the directory in all others the path is fixed, however it has a complication in case of my driver cd player and dvd the letter is j. But in other drivers when inserted the dvd of course the letter will change to f or g or i for example, how to solve this? How exists? because the Type the folder where it will be copied this defined in problem, but how to get and copy all files of this folder specify in or add variants and search type let's assume that the driver is with the letter n as I put in bat to find this folder with those Files and paste inside the folder that I specified? Is it possible?
I have the following .bat: copy j:\dvd folder\*.* "c:\file of destination in the pc. This .bat file recognizes the drive J as if it were in any other PC in a universal way let's say, regardless of which PC the bat is fired it will go into that DVD folder will copy and paste in the specified folder in c: windows windows folder Is not the problem, the problem is fixed the problem is this other unit of dvd in which the letters of the unit change from there will not work when clicking this bat because it is in my pc for example in another it will be i, this is the problem.
You may be able to leverage the WMI command line:
#Echo Off
Set "CDROM="
For /F "Skip=1 Delims=" %%A In (
'"WMIC CDROM Where (MediaLoaded='TRUE') Get Drive 2>NUL"'
) Do For %%B In (%%A) Do If Exist "%CDROM%\DVD Folder\" Set "CDROM=%%B"
If Not Defined CDROM GoTo :EOF
Copy "%CDROM%\DVD Folder\*.*" "%SystemDrive%\Somewhere Existing"
Timeout -1
Think from another perspective.
#echo off
copy "%~Dp0Dvd Folder\*.*" "C:\Folder\"
rem We don't need \ in between %~Dp0 and Dvd Folder because %~Dp0 comes with a \
Place this batch file in your DVD root folder.
Explanation:
%~Dp0
I need to copy files on a specific shared drive. However, some people are mapped to the N: Drive while others are mapped to the T:Drive and so on. There is a specific \server\share used to specify the drive mapping.
What I normally do is copy a file from the shared drive (N:Drive) to the Hard Disk (C:Drive) but not everyone has the same mapping. Some people have the \server\share mapped to the T:Drive instead of the N:Drive.
So is there a way to have the .batch file bypass the letter mapping of the shared drive and just allow me to search for the \server\share? I'll list an example of my current code below and you tell me how you would do it by bypassing the letter
#echo off
xcopy /y "N:\1.0 Blank Folder\Resource1.url" "C:\Program Files\Ericom Software"
xcopy /y "N:\1.0 Blank Folder\Resource2.msg" "C:\Program Files\Ericom Software"
xcopy /y "N:\1.0 Blank Folder\Resource3.lnk" "C:\Program Files\Ericom Software"
echo ********** INITIAL SETUP COMPLETE **********
Now how do I do that withouth using "N:\1.0 Balnk Folder/Resource3.lnk"? It should be "\server\share\1.0 Blank Folder\Resource3.lnk" instead but that doesn't work, I've already tried it. Any ideas?
Thanks,
Mark
Drive letters are for MSDos compatability, thus are over twenty years since they were needed. Use UNC. \\server\sharename\folder\file.ext.
copy "\\servername\sharename\1.0 Blank Folder\Resource1.url" "C:\Program Files\Ericom Software"
eg
C:\Users\David Candy>copy\\127.0.0.1\C$\windows\win.ini .\win.ini
1 file(s) copied.
I got the answer to my questions. The above is partially correct but did not work because of the quotes. The correct answer is below:
\servername\sharename\"1.0 Blank Folder\Resource1.url" "C:\Program Files\Ericom Software"
Thank you for your help!
I want to copy files from two directories on a remote computer to a single directory on a windows server. In one directory I have files with the extension *.csv and in the other *.asc. Drives have been mapped with the UNC convention so we have drives such as Z:\ which are mapped to a specific folder on another computer. The scripts can reside in either the remote computer or on the windows server.
The files with the *.asc extension are then processed and then need to be renamed or moved to another directory (which our software does). The files with the *.csv extension are not changed
I tried using Robocopy and this worked with the MOVE switch
c:\scripts\ROBOCOPY.exe z:\ C:\files\Magellan /MOV /NP /R:2 /W:2 *.asc >c:\scripts\synchro.log
However, the source programme / software requires that these *.asc remain in the original source location - so I can`t use the move switch.
I don`t want to copy duplicate *.asc files - otherwise these files will be processed again.
So I need to only copy new files - ie files that have not been copied before and compare them to another location where the file has either been renamed or moved !
#echo off
pushd Z:\
for %%# in (*.csv) do (
echo n|copy /-Y "%%~f#" "C:\files\Magellan\"
)
for %%# in (*.asc) do (
echo n|copy /-Y "%%~f#" "C:\files\Magellan\%%~n#.csv"
)
try this.If your language settings are different than english you might need to change the echo n that applies to your language.Not sure if this can be done with robocopy or xcopy (I need to read their help in details).This will copy/move first the .csv files and then the .asc files . You can change the order and where is needed you can replace the copy command with move
Pick the order you want to run these lines in (and fix up the mapped drive letters). That said, I'm not sure that you just want to copy both csv and asc files to the Magellan directory as your question mentions comparing them to another location.
echo n|copy /-y z:\*.csv c:\files\Magellan
echo n|copy /-y y:\*.asc c:\files\Magellan\*.csv
I have a back up batch file that will backup my files and create a zip file out of them, then move this zip file to a USB drive that will be plugged into the PC, the USB drive could be allocated a drive letter between E to H, dependant on what else is using the ports, the below code works on a Windows 7 PC but not on a Windows XP machine, which the Batch File is going to be used on
The USB drive letter changes depending on which machine I'm backing up and /or how many devices are connected and using drive letters, I don't want to get a choice, just move the file to whatever drive is plugged in as there will only ever be one drive plugged in at a time as it is only used for back up purposes
How can I make this work on an XP machine, it will only be used on an XP machine, also can I throw up and error if a USB drive is not found and delete the zip file, as this is all running from a CF card, embedded system, so space is limited to save any files
REM ------ Creation of a date stamp for the ZIP file on the USB and set the File Name Structure------
set DT=%DATE:/=-%
set timestamp=%DT:~7,3%%DT:~4,3%%DT:~10,4%
Set FileStamp=%BackupName%-%timeStamp%
REM ------ Creation of the ZIP file ------
%SupervisorPath%\7-ZipPortable\App\7-Zip\7z a -tzip %BackupPath%\Backup\%FileStamp%.zip %BackupPath%\Backup\
REM ------ Move the backup file to a USB drive with File Name and Date Stamp ------
for %%D in (E F G H) do if exist %%D: (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
:break
Put on your USB drive in root a file for example with name BackupDrive.txt. The content does not matter, but I suggest to store in the file something like
File to identify the USB backup drive. Never delete this file.
Set the hidden file attribute on this file.
In your batch file use:
for %%D in (E F G H) do if exist %%D:\BackupDrive.txt (
echo Moving files to USB drive %%D:
move /y "%BackupPath%\Backup\%FileStamp%.zip" %%D: >nul && (
echo Files moved to USB drive successfully
goto :break
)
)
Or most likely better, you give your USB drive a unique label and find the drive by label, see
How to reference a volume/drive by label?
How to refer to/select a drive based only on its label (i.e. not the drive letter)?