Page 1 of 4
Need Batch Script - New Question
Posted: 17 Feb 2013 10:01
by MeSH
New QuestionI need a code that record what folder was been deleted by using this code:
I use this additional code but no luck... not working it's just create a log file
Code: Select all
rmdir /s /q "Folder1\Folder" >> "UserBackup\deleteLog.log"
It is possible to make a log file that contains what folders has been deleted?
Previous Question:Code: Select all
good day/evening to everyone....
can you help me with my problem about batch codes?
I want is:
from Current Directory there are 1 folder and five subfolders
CurrentFolder
---> NewFolder
-------> NewFolder_1
-------> NewFolder_2
-------> NewFolder_3
-------> NewFolder_4
i want to create backup for New Folder 2 & 4 only and make a log file...
how will add the information that files has been backup in one log file?
this is the command that I use when backup-ing a multiple folders:
mkdir UserBackup
xcopy "NewFolder\*.*" "UserBackup\NewFolder\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
this code means that all folders are inside the NewFolder will be copy but I want is only the NewFolder 2 & 4 will only to be copied.
nedd the right code plus explanation.. thanks
Re: Need Batch Script
Posted: 17 Feb 2013 15:07
by abc0502
You already know the folders names, just use the same command twice one for folder 2 and another for folder 4
Code: Select all
MKDIR "UserBackup"
XCopy "NewFolder\NewFolder_2\*.*" "UserBackup\NewFolder\NewFolder_2\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
Re: Need Batch Script
Posted: 17 Feb 2013 16:46
by Ocalabob
Greetings MeSH,
If you would like to 'test drive' Robocopy for backing up your files then consider:
Code: Select all
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_2 c:\UserBackup\NewFolder /s /log+:logfile.log
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_4 c:\UserBackup\NewFolder /s /log+:logfile.log
or if you want to maintain the folder structure in UserBackup then:
Code: Select all
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_2 c:\UserBackup\NewFolder\NewFolder_2 /s /log+:logfile2.log
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_4 c:\UserBackup\NewFolder\NewFolder_4 /s /log+:logfile2.log
Notes:
1. Robocopy for XP or newer OS.
2. If the destination folder/folders do not exist Robocopy will create them.
3. Robocopy will create one detailed log file in the above examples.
4. Many of the switches xcopy requires exist in Robocopy by default.
5. Typing Robocopy /? at a command line gives more information.
A final comment. If you want only one log file using abc0502's solution then change the second to:
Code: Select all
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i >> "UserBackup\Logfile.log"
Best wishes!
Re: Need Batch Script
Posted: 17 Feb 2013 22:40
by MeSH
abc0502 wrote:You already know the folders names, just use the same command twice one for folder 2 and another for folder 4
Code: Select all
MKDIR "UserBackup"
XCopy "NewFolder\NewFolder_2\*.*" "UserBackup\NewFolder\NewFolder_2\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
thanks! I'll try that one
Ocalabob wrote:Greetings MeSH,
If you would like to 'test drive' Robocopy for backing up your files then consider:
Code: Select all
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_2 c:\UserBackup\NewFolder /s /log+:logfile.log
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_4 c:\UserBackup\NewFolder /s /log+:logfile.log
or if you want to maintain the folder structure in UserBackup then:
Code: Select all
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_2 c:\UserBackup\NewFolder\NewFolder_2 /s /log+:logfile2.log
ROBOCOPY c:\CurrentFolder\NewFolder\Newfolder_4 c:\UserBackup\NewFolder\NewFolder_4 /s /log+:logfile2.log
Notes:
1. Robocopy for XP or newer OS.
2. If the destination folder/folders do not exist Robocopy will create them.
3. Robocopy will create one detailed log file in the above examples.
4. Many of the switches xcopy requires exist in Robocopy by default.
5. Typing Robocopy /? at a command line gives more information.
A final comment. If you want only one log file using abc0502's solution then change the second to:
Code: Select all
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i >> "UserBackup\Logfile.log"
Best wishes!
and this too.. the robocopy is new to me... even if I use win 7 -_- ... thanks to all who reply
I'll try this later after making powerpoint presentation
UPDATE:
@abc0502
it works man but the problem is the logfile for NewFolder2 has been replace by the logfile of NewFolder4 what I mean about the logfile... all the files that has been copy on the two folders must be put in one logfile
this is the result:
Code: Select all
NewFolder\NewFolder4\New Text Document (2).txt
NewFolder\NewFolder4\New Text Document (3).txt
NewFolder\NewFolder4\New Text Document.txt
3 File(s) copied
it must be
Code: Select all
NewFolder\NewFolder2\New Text Document (2).txt
NewFolder\NewFolder2\New Text Document (3).txt
NewFolder\NewFolder2\New Text Document.txt
NewFolder\NewFolder4\New Text Document (2).txt
NewFolder\NewFolder4\New Text Document (3).txt
NewFolder\NewFolder4\New Text Document.txt
6 File(s) copied
next i'll try the code that Ocalabob post update later
UPDATE 2:
sir! this is what I want!
Code: Select all
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Feb 18 13:08:37 2013
Source : C:\Users\UserName\Desktop\test\NewFolder\Newfolder2\
Dest : C:\Users\UserName\Desktop\test\UserBackup\NewFolder\NewFolder2\
Files : *.*
Options : *.* /S /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
New Dir 3 C:\Users\UserName\Desktop\test\NewFolder\Newfolder2\
New File 0 New Text Document (2).txt
100%
New File 0 New Text Document (3).txt
100%
New File 0 New Text Document.txt
100%
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 1 0 0 0 0
Files : 3 3 0 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Mon Feb 18 13:08:37 2013
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Feb 18 13:08:37 2013
Source : C:\Users\UserName\Desktop\test\NewFolder\Newfolder4\
Dest : C:\Users\UserName\Desktop\test\UserBackup\NewFolder\NewFolder4\
Files : *.*
Options : *.* /S /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
New Dir 3 C:\Users\UserName\Desktop\test\NewFolder\Newfolder4\
New File 0 New Text Document (2).txt
100%
New File 0 New Text Document (3).txt
100%
New File 0 New Text Document.txt
100%
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 1 0 0 0 0
Files : 3 3 0 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Mon Feb 18 13:08:37 2013
but can I edit the way ROBOCOPY make logfile? because when a beginner will view this it will be a long logfiles and confusing.... cause the folder i will make backup will contains hundreds of files.... can I edit that logfile like on the post on @abc0502 quote?
UPDATE 3
i tried the code of abc0502 and plus the code you provide...
and here's the result:
Code: Select all
NewFolder\NewFolder4\New Text Document (2).txt
NewFolder\NewFolder4\New Text Document (3).txt
NewFolder\NewFolder4\New Text Document.txt
3 File(s) copied
NewFolder\NewFolder2\New Text Document (2).txt
NewFolder\NewFolder2\New Text Document (3).txt
NewFolder\NewFolder2\New Text Document.txt
3 File(s) copied
this the kind of logfile i want
thanks to everyone who help me can I ask for more questions on this thread? I don't like to create new one... oh.. btw is there a spoiler code here?
@Ocalabob
on this code:
Code: Select all
XCopy "NewFolder\NewFolder4\*.*" "UserBackup\NewFolder\NewFolder4\*.*" /c /s /r /d /y /i > "UserBackup\Logfile.log"
and this
Code: Select all
XCopy "NewFolder\NewFolder4\*.*" "UserBackup\NewFolder\NewFolder4\*.*" /c /s /r /d /y /i >> "UserBackup\Logfile.log"
what is the diffrence? if I add one more greater than symbol? can you explain sir? because this is the code I use above to make logfile....
Re: Need Batch Script
Posted: 18 Feb 2013 03:39
by abc0502
Hi, Sorry it's my mistake, i copied the whole line and didn't change the log file name so it replaced the previous one,
you will just have to change the log file name for the 2nd file, you can try this.
Code: Select all
MKDIR "UserBackup"
XCopy "NewFolder\NewFolder_2\*.*" "UserBackup\NewFolder\NewFolder_2\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_4.log"
I changed the log file names so you each folder will have a log file containing it's name.
Robocopy is a bit confusing at the first, i prefer XCopy when backing files.
Try reading RoboCopy help docs.
Re: Need Batch Script
Posted: 18 Feb 2013 04:45
by MeSH
abc0502 wrote:Hi, Sorry it's my mistake, i copied the whole line and didn't change the log file name so it replaced the previous one,
you will just have to change the log file name for the 2nd file, you can try this.
Code: Select all
MKDIR "UserBackup"
XCopy "NewFolder\NewFolder_2\*.*" "UserBackup\NewFolder\NewFolder_2\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
XCopy "NewFolder\NewFolder_4\*.*" "UserBackup\NewFolder\NewFolder_4\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_4.log"
I changed the log file names so you each folder will have a log file containing it's name.
Robocopy is a bit confusing at the first, i prefer XCopy when backing files.
Try reading RoboCopy help docs.
my question is solve now
i just combine yours and Ocalabob and boom! all changes are in one log-file
many thanks!
my second question and favor...
how will I do this in batch script? and is this possible?
when my patcher is start to run it this will be pop-up first: then the folder I chose must be where the files must be move...
is this level of code is hard? or meduim? or an easy one?
Re: Need Batch Script
Posted: 18 Feb 2013 04:59
by abc0502
Unfortunately, Batch files can't do Graphical Interface but VBscript can,
you can combine vbscript and batch files to do that.
I found this vbscript it give you what you need i will see if i can combine it, but post your code first to work on.
Link
Re: Need Batch Script
Posted: 18 Feb 2013 05:24
by MeSH
abc0502 wrote:Unfortunately, Batch files can't do Graphical Interface but VBscript can,
you can combine vbscript and batch files to do that.
I found this vbscript it give you what you need i will see if i can combine it, but post your code first to work on.
Link
here's my plan code:
this is sample only
Code: Select all
@echo off
title Test
:MAIN
echo Do you want to start the patch? (Y/N)
set/p "cho=>"
if %cho%==Y goto INSTALL1
if %cho%==y goto INSTALL1
if %cho%==n goto EXIT
if %cho%==N goto EXIT
echo Invalid choice.
goto MAIN
:INSTALL
echo Preparing files. Please wait...
ping localhost -n 5 > nul
echo Starting to patch now. Please wait for a moment...
ping localhost -n 3 > nul
cls
REM This next code must be trigger on where you choose on the Browse Folder Dialog box
xcopy "NewFolder(the main folder which has all files in it)\NewFolder1\*.*" "UserBackup\NewFolder\NewFolder1\*.*" /c /s /r /d /y /i >> "UserBackup\Logfile.log"
REM Userbackup <---- this is the folder I chose and it will paste all files
cls
echo
echo Thank Your for using my MOD.
echo.
echo for more details please visit my Home Page: http://example.com
echo.
echo Presss any key to continue...
echo. Special Thanks to:
echo abc0502
echo Ocalabob
echo.
echo for helping me, more POWER to these guys!
pause>nul
exit
by the way sir I'm just a first year Info Tech.. so C language is our first program.. but I got interest in batch script when I start to make a modification of a game.
Re: Need Batch Script
Posted: 18 Feb 2013 05:51
by abc0502
There is a problem here, you will not be able to backup all your folders at once, you will have to back up your newfolder_2 first and after done you will then run the vbscript again to input the 2nd folder and so for each folder.
is that ok ?
Re: Need Batch Script
Posted: 18 Feb 2013 05:58
by MeSH
abc0502 wrote:There is a problem here, you will not be able to backup all your folders at once, you will have to back up your newfolder_2 first and after done you will then run the vbscript again to input the 2nd folder and so for each folder.
is that ok ?
hmm sorry sir I don't get it...can you clarify it? this is how I understand it:
1. choose folder
2. the patch will extarct all the files in one folder
3. the patcher will back-up all files and folders that are encode on it
4. after the back-up done. then the patcher will trigger the folder that is extracted and copy to the one folder I chose
so you mean is
when the main folder finish to extract then inside are have 3 folders then
then when backing up many folders then it will back up one by one and start all over again until the back-up is finish?
Re: Need Batch Script
Posted: 18 Feb 2013 07:06
by abc0502
Now i'm lost, your batch's main job is to backup folders.
and you need the graphical interface to choose the destination location instead of writing it in the batch file.
Is that right ?
If that is right, and if you have more than one back up operation to do then you will have to provide different location as each destination location must have different names or newfolder_2 as an example will be backed in the same location as newfolder_4.
so the script will not be fully automated, as for each folder need backup you will have to run the vbscript+batch file every time for every folder
Re: Need Batch Script
Posted: 18 Feb 2013 07:42
by abc0502
Ok, Never mind the previous post
At First you can replace These lines:
Code: Select all
if %cho%==Y goto INSTALL1
if %cho%==y goto INSTALL1
if %cho%==n goto EXIT
if %cho%==N goto EXIT
with
Code: Select all
if /i %cho%==Y goto INSTALL1
if /i %cho%==N goto EXIT
the /i switch will ignore the letter case ( capital or small) and will treat both as the same.
also add
set "cho=" before
set /p "cho=>" to prevent any crashes when the user input nothing and press enter directly.
Now this what you are going to do after you finish coding your batch:1> After the Exit command add this function:Code: Select all
:GUI <variable_to_set_destinatio_to>
IF Exist "output1256.txt" DEL /F /Q "output1256.txt" >NUL
rem Create the VBScript, if not exist
IF NOT EXIST "%~DP0GUI.vbs" (
( FOR /F "tokens=1*" %%a IN ('findstr "^:VBS: " ^< "%~F0"') DO Echo.%%b )>"%~DP0GUI.vbs"
)
Cscript //nologo "%~DP0GUI.vbs"
For /F "delims=" %%A In ('Type "OutPut.txt"') Do SET "%~1=%%A"
GOTO :EOF
:VBS: Option Explicit
:VBS: BrowseFolder "My Computer", False
:VBS: 'Source/Destination Folder
:VBS: Function BrowseFolder( myStartLocation, blnSimpleDialog )
:VBS: Const MY_COMPUTER = &H11&
:VBS: Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
:VBS: Dim numOptions, objFolder, objFolderItem
:VBS: Dim objPath, objShell, strPath, strPrompt,oFS, objShell2, fsHandle
:VBS: strPrompt = "Select a Folder:"
:VBS: numOptions = &H10& ' Additional text field to type folder path
:VBS: Set objShell = CreateObject( "Shell.Application" )
:VBS: If UCase( myStartLocation ) = "MY COMPUTER" Then
:VBS: Set objFolder = objShell.Namespace( MY_COMPUTER )
:VBS: Set objFolderItem = objFolder.Self
:VBS: strPath = objFolderItem.Path
:VBS: Else
:VBS: strPath = myStartLocation
:VBS: End If
:VBS: Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
:VBS: If objFolder Is Nothing Then
:VBS: BrowseFolder = ""
:VBS: Exit Function
:VBS: End If
:VBS: Set objFolderItem = objFolder.Self
:VBS: objPath = objFolderItem.Path
:VBS:
:VBS: set oFS = WScript.CreateObject("Scripting.FileSystemObject")
:VBS: Set objShell2 = CreateObject("wscript.shell")
:VBS: Set fsHandle = oFS.OpenTextFile ("output1256.txt",8,True) 'external file
:VBS: fsHandle.Writeline objPath
:VBS: End Function
REM Must leave empty line after this line
and don't forget to leave empty line at the end of your batch file it's important.
2> before each Xcopy command in your batch add this command:The word
"Destination_Folder" can be any word you want but this will be the variable that will hold the destination folder name. ( leave it as it is for now )
3>now replace all destination folders in all xcopy commands like the red part here:XCopy "NewFolder\NewFolder_2\*.*" "UserBackup\NewFolder\NewFolder_2\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
with this:
And that's all, it should work find with you, and for each xcopy command you will get a pop-up GUI to ask for destination folder.
This Link has many usiful GUI interfaces that can be used with batch files too :
www.robvanderwoude.com
Re: Need Batch Script
Posted: 18 Feb 2013 08:01
by MeSH
abc0502 wrote:Now i'm lost, your batch's main job is to backup folders.
and you need the graphical interface to choose the destination location instead of writing it in the batch file.
Is that right ?
If that is right, and if you have more than one back up operation to do then you will have to provide different location as each destination location must have different names or newfolder_2 as an example will be backed in the same location as newfolder_4.
so the script will not be fully automated, as for each folder need backup you will have to run the vbscript+batch file every time for every folder
sorry for my bad english... it's not just backuping...
the back-up is the folder to be replaced so that when the user is using my patch is not satisfied to my work then they have a backup so that they can replace it anytime they want...
here download this
code... hope you get it what i mean..
Re: Need Batch Script
Posted: 18 Feb 2013 08:04
by MeSH
is this correct sir?
Code: Select all
@echo off
title TEST
:MAIN
echo Testing files (Y/N)
set "cho="
set /p "cho=>"
if /i %cho%==Y goto INSTALL1
if /i %cho%==N goto EXIT
echo Invalid choice
goto MAIN
:INSTALL1
mkdir UserBackup
CALL :GUI "Destination_Folder"
XCopy "NewFolder\NewFolder_2\*.*" "%Destination_Folder%\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
pause>nul
:EXIT
exit
:GUI <variable_to_set_destinatio_to>
IF Exist "output1256.txt" DEL /F /Q "output1256.txt" >NUL
rem Create the VBScript, if not exist
IF NOT EXIST "%~DP0GUI.vbs" (
( FOR /F "tokens=1*" %%a IN ('findstr "^:VBS: " ^< "%~F0"') DO Echo.%%b )>"%~DP0GUI.vbs"
)
Cscript //nologo "%~DP0GUI.vbs"
For /F "delims=" %%A In ('Type "OutPut.txt"') Do SET "%~1=%%A"
GOTO :EOF
:VBS: Option Explicit
:VBS: BrowseFolder "My Computer", False
:VBS: 'Source/Destination Folder
:VBS: Function BrowseFolder( myStartLocation, blnSimpleDialog )
:VBS: Const MY_COMPUTER = &H11&
:VBS: Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
:VBS: Dim numOptions, objFolder, objFolderItem
:VBS: Dim objPath, objShell, strPath, strPrompt,oFS, objShell2, fsHandle
:VBS: strPrompt = "Select a Folder:"
:VBS: numOptions = &H10& ' Additional text field to type folder path
:VBS: Set objShell = CreateObject( "Shell.Application" )
:VBS: If UCase( myStartLocation ) = "MY COMPUTER" Then
:VBS: Set objFolder = objShell.Namespace( MY_COMPUTER )
:VBS: Set objFolderItem = objFolder.Self
:VBS: strPath = objFolderItem.Path
:VBS: Else
:VBS: strPath = myStartLocation
:VBS: End If
:VBS: Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
:VBS: If objFolder Is Nothing Then
:VBS: BrowseFolder = ""
:VBS: Exit Function
:VBS: End If
:VBS: Set objFolderItem = objFolder.Self
:VBS: objPath = objFolderItem.Path
:VBS:
:VBS: set oFS = WScript.CreateObject("Scripting.FileSystemObject")
:VBS: Set objShell2 = CreateObject("wscript.shell")
:VBS: Set fsHandle = oFS.OpenTextFile ("output1256.txt",8,True) 'external file
:VBS: fsHandle.Writeline objPath
:VBS: End Function
REM Must leave empty line after this line
sir after i choose the path.. example at drive C:? then an error appear.. the system cannot find the file specified
Re: Need Batch Script
Posted: 18 Feb 2013 08:22
by abc0502
as the source folder ( the one that will be backed) is always known so change this command:
Code: Select all
XCopy "NewFolder\NewFolder_2\*.*" "%Destination_Folder%\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
to:
XCopy "NewFolder\NewFolder_2\*.*" "%Destination_Folder%\NewFolder_2\*.*" /c /s /r /d /y /i > "%Destination_Folder%\Logfile_NewFolder_2.log"
and you can remove the MKDIR command, it will be useless
And Always keep empty line at the end of your batch file