I am working on creating a backup program using python and batch scripts. The entire program will work if I don't have the program export a copy of the incremental backup to a shared network folder(I want to have redundancy so I have it save to server and two other terminals). I can manually go into cmd prompt and type out the ren cmd and it will do it without any issue. The problem arises when I attempt to use a variable with the ren cmd.
Example of set /p
REM Load text file with first variable
set loadDD002=C:\Backup\Bin\DD002.txt
SET /p back1Directory=<%LoadDD002%
REM Show the Defined Variable
echo back1Directory
Example of contents of DD002.txt
//SERVER/NetShare
example of cleanup/renaming
del "%back2Directory%\backup7.zip"
ren "%back2Directory%\backup6.zip" backup7.bak
ren "%back2Directory%\backup5.zip" backup6.zip
ren "%back2Directory%\backup4.zip" backup5.zip
ren "%back2Directory%\backup3.zip" backup4.zip
ren "%back2Directory%\backup1.zip" backup2.zip
ren "%back2Directory%\backup.zip" backup1.zip
Error receiving
The syntax of the command is incorrect
I apologize for any editing faux pas as I am very new to this site.
Thank you,
Nvm.. I figured it out. See below:
set "network1=%back1Directory:* =%
Then
set "network1=%back1Directory:* =%
WOOO
Related
I have a folder xyz. It has a batch file which copies files from source directory abc to destination directory def. I am using copy with options /v /y.
Copy works absolutely fine. But I notice a strange or weird issue that additionally a copy of all the files copied from source to destination are present in folder xyz.
I started observing this issue after a system restart and not sure if its a one time issue. But I would like to know if someone has run into this issue before and what is the possible fix?
Here is the code:
#if not defined ECHO_ON echo off
SETLOCAL EnableDelayedExpansion
set arg1=%1
set arg2=%2
copy /v /y !arg1! !arg2!
call :getPath !arg1!
ren !arg2!\!_NAME_EXT! !_NAME!.svg
:getPath
set _NAME=%~n1
set _NAME_EXT=%~nx1
set _LOC=%~dp1
goto:eof
endlocal
Please note I am using copy and robocopy command (for some other copying operation) in same .bat file.
Is this something to be worried about?
(As I wrote things worked fine until restart.)
Your double Copy is because a batch script works line by line until it reaches an end of file marker or an exit instruction. A Call command returns back to the point just after the Call instruction. When it returns, there is no exit instruction or end of file marker until the bottom of your script, so the :getPath label is executed again.
There appears to have been absolutely no reason for EnableDelayedExpansion in your script, for Setting any variables or for a Call command. I have therefore simplified it as such:
#Echo Off
If "%~2"=="" Exit /B
If Not Exist "%~2\" MD "%~2" 2>Nul || Exit /B
If Exist "%~1" Copy /V /Y "%~1" "%~2\%~n1.svg"
I hope it helps you out.
I am trying to create a .BAT file in DOS 6.22 that will copy the contents of a floppy disk in A: over to C:\ and then set the folder created as a system variable. I tried using something like "SET /P VARIABLE=Enter a path" however DOS will just add "/P VARIABLE" as a variable with the value of "Enter a path" so using the /P isn't an option as /P wasn't a switch in DOS 6.22
I tried using something like a for loop to set a variable to the file however where I hit a speed bump is that I have no idea what the folder is going to be called in drive A:\ as it will change all the time but only ever contain one folder, so basically I am just trying to find a way a way to copy the first directory found in drive A over to C:\ and set that as a system variable. As once the user is done making changes I will have to copy that folder back over to A:\ and overwrite the old files so it can be stored on the network once changes have been made.
I did try experimenting with some If/for statements through a .BAT file but I didn't have much luck with theses, if anyone could point me in the right direction that would be awesome.
At this point I'm probably making this way more complicated than I have to.
something like this should work too:
#echo off
:INPUT.BAT puts what is typed next in environment variable INPUT
set input=
echo INPUT.BAT
echo Type in something and press [Enter]
fc con nul /lb1 /n|date|find " 1: ">temptemp.bat
echo :Loop>>enter.bat
echo if not (%%input%%)==() set input=%%input%% %%5>>enter.bat
echo if (%%input%%)==() set input=%%5>>enter.bat
echo shift>>enter.bat
echo if not (%%5)==() goto Loop>>enter.bat
for %%x in (call del) do %%x temptemp.bat
del enter.bat
echo The string you just entered:
echo %input%
echo has been stored in an environment variable named INPUT
:End
I ended finding a solution after doing some more research from what #Squashman linked. Turns out there was a breakdown in communication and this wasn't even the original issue that the user had (a simple way to copy the files off A:\ and all that)
I used the following.
echo Type "set myvar="name of the folder" replacing name of the folder with
echo the name of the folder containing the files on A:\ example if you were
echo on "example" you would type: set myvar=example
copy con answer.bat
echo Type the words "set myvar=" (don't type the quote marks)
echo and then immediately after the = sign, press Control-Z then enter
call answer.bat
mkdir C:\%myvar%
xcopy A:\%mvar% C:\%myvar%
DEL answer.bat
This is a modified version of a guide I found here. http://www.pement.org/sed/bat_env.htm#4dos
Hopefully this is able to help someone, this isn't pretty by any stretch of the imagination but it worked.
I think this would be easier for your users if they actually need to only copy one directory.
#echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1
goto end
:syntax
echo Please input a directory after %0
:end
I'm new in batch programming. The thing is I have a path and a new folder name in 2 variables, so I want to concatenate it and make a new folder in that result path. I tried many things but nothing worked. Please help
I tried this code
#echo off
setlocal EnableDelayedExpansion
set ver=project
set spath=d:\a\svn\
set path=!%spath%%ver%!
mkdir %path%
pause
endlocal
Do not use path as name for an environment variable because such an environment variable is defined already by default with a very important meaning, see the answers on What is the reason forĀ 'sort' is not recognized as an internal or external command, operable program or batch file?
To concatenate two environment variable values just reference those two environment variables on assigning the value to one of the two environment variables or a new environment variable.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ProjectVersion=project"
set "SvnPath=d:\a\svn\"
set "ProjectPath=%SvnPath%%ProjectVersion%"
mkdir "%ProjectPath%"
pause
endlocal
See also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for the reason using set "variable=value" with double quotes around string value to variable assignment, i.e. around the argument string of command SET.
The commands SETLOCAL and ENDLOCAL would not be really necessary here.
Possible would be also:
#echo off
set "ProjectVersion=project"
set "SvnPath=d:\a\svn\"
set "ProjectPath=%SvnPath%%ProjectVersion%"
mkdir "%ProjectPath%" 2>nul
if not exist "%ProjectPath%\" echo Failed to create directory "%ProjectPath%" & pause & goto :EOF
The batch file above creates the directory with suppressing any error message by redirecting STDERR to device NUL. An error message is output if the directory already exists or it was not possible to create the directory because NTFS permissions denies folder creation for current user or in directory path there is a file with the name of a directory in path, e.g. there is a file with name project in directory d:\a\svn or there is a file svn in directory d:\a. The next command with a backslash appended to directory path checks if the directory exists after execution of command MKDIR and outputs an error message with PAUSE and next exiting batch file when the directory still does not exist.
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and Single line with multiple commands using Windows batch file for an explanation of & operator.
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.
echo /?
endlocal /?
goto /?
if /?
mkdir /?
pause /?
set /?
setlocal /?
#echo off
setlocal EnableDelayedExpansion
set version=project
set spath=d:\a\svn\
set mypath=%spath%%version%
mkdir %mypath%
pause
endlocal
path is a reserved name - it defines the sequence of directories that is searched for an executable if the executable is not found in the current directory. If you change it, well - in short, gloom
ver is not a reserved name, but it is the name of a inbuilt utility and makes a poor choice for variable-name.
Your code was attempting to set your desired new pathname to the contents of the variable d:\a\svn\project. Since this variable is very unlikely to exist, you would have attempted to make a directory named nothing.
btw - there is no need to set mypath - md %spath%%ver% would work just as well. MD is a synonym of mkdir and is used more often.
Having troubles with cmd syntax, trying to delete files with specific extension in a specific folder that don't have sidecars. eg.: if folder contains:
1.A, 1.B, 2.A, 3.A, 4.A, 4.B
bat should only delete
2.A, 3.A
..hope that makes sense.
The code I've got so far must be real close, unfortunatelly not working
#echo off
FOR %%x IN (%1\*.A) DO
(
IF not exist "%1\%x.B" del "%1\%x.A"
)
Any help mostly appreciated.
Comments to question are correct
FOR variable percents must be consistently doubled when used in batch script
Open paren must be on same line as DO. But there isn't any need for parens with such a simple script.
Also, you want only the base name of the FOR variable, so you need the ~n modifier.
I made the code a bit more robust by using PUSHD at beginning.
#echo off
pushd %1
for %%F in (*.A) do if not exist "%%~nF.B" del "%%F"
popd
I'm a mortgage loan guy in Seattle, WA and I frequently set up a folder hierarchy into which I save a client's documents and as they come in to me. I've been creating these manually for years and I'd like to save the 3 to 4 minutes it takes to set these up by using a batch file.
So... I have a default set of folders, some of which contain a couple of small Adobe PDFs. What I'd like to do (and cannot make happen) is to run a batch file that would facilitate some custom remarks or input from me during the batch so that with a click and a couple of keystrokes, I have an organized folder setup for a new client within seconds rather than minutes.
I've written the following but it isn't producing any output folders or files.
______not sure character terms show correctly - see linked images below for actual______
#echo off
::Ask
echo Your Source Path:
set INPUT1=
set /P INPUT1=Type input:
echo Your Destination Path:
set INPUT2=
set /P INPUT2=Type input:
C:\WINDOWS\system32\xcopy.exe /e /v %INPUT1% %INPUT2%
My responses were:
to the first prompt "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014"
to the second prompt "E:\DV8333 MY DOCUMENTS\001 CLIENTS\"
I have verified that xcopy.exe is in fact located as indicated above.
I'm on XP SP3
My actual paths and .bat file are shown in the linked image for clarity.
http://www.avidrecording.com/images/01.png
Thanks in advance, much appreciated.
#Rem save this as a .bat file and run
#echo off
set /P source=Enter Source Folder:
echo %Source%
set /P destination=Enter Destination Folder:
echo %destination%
xcopy %source% %destination% /v /i /e
Fundamentally, your problem appears to be that the xcopy command can't figure out which of the data it is receiving is parameters and which switches and which superfluous because you have spaces in your directorynames.
Fortunately, the cure is simple. "quote your parameters"
C:\WINDOWS\system32\xcopy.exe /e /v "%INPUT1%" "%INPUT2%"
Now - the path to xcopy.exe is probably superfluous - as is the extension, so
xcopy /e /v "%INPUT1%" "%INPUT2%"
is more than likely all you'd need.
(I'd caution to experiment with a throw-away destination until you've perfected your method. I use a RAMDISK...)
Also, if you're copying a template, then there's no apparent reason for all the folderol about inputting Input1. If you have more than one template set, set up a separate batch and shortcut for each one with a fixed template path, eg
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "%INPUT2%"
Note the use of quotes to defeat the evil spaces.
Next, your destination could be built, but may contain spaces. For instance, you may have a client to which you wish to refer as "037 - Fred Nurk". Now it's a pain to have to type in the full path, so make it easy. Just type in the 037 - Fred Nurk part and let batch fill in the rest.
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "E:\DV8333 MY DOCUMENTS\001 CLIENTS"\"%input2%"\
Note that this will append the input as a directory under E...001 clients. Note that the strings are concatenated and the double-quotes are there solely to tell batch "here be a string that may contain spaces."
If this works, and there's no reason why it wouldn't (does for me...) then all you'd need to do is enter the client details and the template would be copied to a new directory. Now actually playing around with the data in the files that are copied so that they are customised - well, at the price, that would be worthy of another question...
#echo off
echo Backing up file
set /P source=Enter source folder:
set /P destination=Enter Destination folder:
set /P Folder name=Enter Folder name
set xcopy=xcopy // Set the switches as per your need
%xcopy% %source% %destination%
pause