New to making batch files some help would be awesome.
Moderator: DosItHelp
New to making batch files some help would be awesome.
Hi all,
I'm new here and have been trying to learn how to make batch files for work and home use.
Been reading / watching videos but would like your help if possible to achieve something my current level of ability cannot.
Scenario :
I have a server sbs 2008 with a mapped drive *documents* inside this folder I have a folder named paperport inside that 2 folders (among others that i do not want to touch) 1 named Jobs and one named Quotes, inside these folders are jobs folders and quotes folders I want to move old jobs / quotes folders to an external drive at a certain time, once moved I would like those folders to be deleted in the original location and a new set of folders created.
Here is the file structure currently
Jobs > Jobs > 24400-24449 > 24400 > files relating to that job etc
> 24401 > files relating to that job etc
> 24402 > files relating to that job etc
> 24403 > files relating to that job etc
etc upto
> 24449 > files relating to that job etc
Jobs > jobs > 24450-24499
Jobs > jobs > 24500-24549
etc etc
Once I have reached the folder limit i usually just create the new folder and sub folders myself but would like to automate this at the same time of moving the oldest jobs batch to an archive.
so for the above i would want \\server\documents\paperport\jobs\jobs\24400-24449 and all its contents moved to \\mediaserver\archives\jobs
Once moved \\server\documents\paperport\jobs\jobs\24400-24449 to be deleted and \\server\documents\paperport\jobs\jobs\24550-24599 to be created with the folders inside it 24550 24551 24552 etc etc.
I dont know if this is possible as each time i would run the file the folder being moved will be a different name and the folder to be created will be different to?
Currently my level of understanding is very basic so anyhelp would be greatly appreciated
I'm new here and have been trying to learn how to make batch files for work and home use.
Been reading / watching videos but would like your help if possible to achieve something my current level of ability cannot.
Scenario :
I have a server sbs 2008 with a mapped drive *documents* inside this folder I have a folder named paperport inside that 2 folders (among others that i do not want to touch) 1 named Jobs and one named Quotes, inside these folders are jobs folders and quotes folders I want to move old jobs / quotes folders to an external drive at a certain time, once moved I would like those folders to be deleted in the original location and a new set of folders created.
Here is the file structure currently
Jobs > Jobs > 24400-24449 > 24400 > files relating to that job etc
> 24401 > files relating to that job etc
> 24402 > files relating to that job etc
> 24403 > files relating to that job etc
etc upto
> 24449 > files relating to that job etc
Jobs > jobs > 24450-24499
Jobs > jobs > 24500-24549
etc etc
Once I have reached the folder limit i usually just create the new folder and sub folders myself but would like to automate this at the same time of moving the oldest jobs batch to an archive.
so for the above i would want \\server\documents\paperport\jobs\jobs\24400-24449 and all its contents moved to \\mediaserver\archives\jobs
Once moved \\server\documents\paperport\jobs\jobs\24400-24449 to be deleted and \\server\documents\paperport\jobs\jobs\24550-24599 to be created with the folders inside it 24550 24551 24552 etc etc.
I dont know if this is possible as each time i would run the file the folder being moved will be a different name and the folder to be created will be different to?
Currently my level of understanding is very basic so anyhelp would be greatly appreciated
Re: New to making batch files some help would be awesome.
Sorry i don't have much time, so i'll take it short:
If you have sufficient rights, then just use:
- "xcopy \\server\documents\paperport... \\mediaserver\archives... /E /V ..." to copy the files and directories, see "xcopy /?" for mor switches.
- "rd /S /Q \\server\documents\paperport..." to delete the directories, see "rd /?"
And for the "number" problem, assumed that "\\server\documents\paperport\jobs\jobs\" only has one subfolder (in this case "24400-24449"), there are always 50 subfolders to be created, and the numbers don't exceed max signed integer value:
(Untested, may contain flaws. I hope this will help you.)
penpen
If you have sufficient rights, then just use:
- "xcopy \\server\documents\paperport... \\mediaserver\archives... /E /V ..." to copy the files and directories, see "xcopy /?" for mor switches.
- "rd /S /Q \\server\documents\paperport..." to delete the directories, see "rd /?"
And for the "number" problem, assumed that "\\server\documents\paperport\jobs\jobs\" only has one subfolder (in this case "24400-24449"), there are always 50 subfolders to be created, and the numbers don't exceed max signed integer value:
Code: Select all
@echo off
:: old from, to values
for %%a in (from to) do set "%%a="
for /F "tokens=* delims=" %%a in ('dir /B "\\server\documents\paperport\jobs\jobs\"') do (
for /F "tokens=1,2 delims=-" %%b in ("%%a") do (
set "from=%%b"
set "to=%%c"
)
)
:: backup commands with this subfolder "%from%-%to%" (xcopy ...)
:: delete directory, and all its subfolders (rd /S /Q ...)
set /A "from+=50", "to+=50"
md "\\server\documents\paperport\jobs\jobs\%from%-%to%"
for /L %%a (%from%, 1, %to%) do md "\\server\documents\paperport\jobs\jobs\%from%-%to%\%%a"
penpen
Re: New to making batch files some help would be awesome.
Hi thanks for the reply.
I woul be lieing if i did not say your post confused me a little
.
Inside "\\server\documents\paperport\jobs\jobs\" there are upwards of 10 folders
Currently inside the jobs folder are the following folders and inside each of those are 50 seperate job folders within the number range of the parent folder.
24400-24449
24450-24499
24500-24549
24550-24599
24600-24649
24650-24699
24700-24749
24750-24799
24800-24849
24850-24899
24900-24949
24950-24999
25000-25049
25050-25099
25100-25149
I would like to keep say the 10 latest job folders and move any older than that to the archive.
that is what i have so far it is very simple and only copies to the new location
I woul be lieing if i did not say your post confused me a little

Inside "\\server\documents\paperport\jobs\jobs\" there are upwards of 10 folders
Currently inside the jobs folder are the following folders and inside each of those are 50 seperate job folders within the number range of the parent folder.
24400-24449
24450-24499
24500-24549
24550-24599
24600-24649
24650-24699
24700-24749
24750-24799
24800-24849
24850-24899
24900-24949
24950-24999
25000-25049
25050-25099
25100-25149
I would like to keep say the 10 latest job folders and move any older than that to the archive.
Code: Select all
@echo off
cls
echo press any key to continue backup!
pause
xcopy C:\Users\Craig\desktop\*.* C:\Users\Craig\test\*.* /s /e /y
echo backup complete
pause
that is what i have so far it is very simple and only copies to the new location
Re: New to making batch files some help would be awesome.
No problempeonowns wrote:Hi thanks for the reply.

Just post, what is confusing you, and i may help.peonowns wrote:I woul be lieing if i did not say your post confused me a little.
I've redesigned my code a little bit, changed the new assumptions (>= 10 folders, ...), and hope the comments are clear:
Code: Select all
@echo off
setlocal enableDelayedExpansion
:: initialize variables
for %%a in (first last archive folder from to) do set "%%a="
set "amount=0"
:: get the first, the last, and the archive () job number
for /F "tokens=* delims=" %%a in ('dir /A:D /B /ON "\\server\documents\paperport\jobs\jobs\"') do (
if not defined first set "first=%%~a"
set "last=%%~a"
)
set "last=%last:*-=%
set /A "first=%first:-=+0*%", "archive=last-10*50"
:: archive files:
for /L %%a in (%first%, 50, %archive%) do (
set /A "from=%%a", "to=%%a+49"
set "folder=!from!-!to!"
echo(xcopy "\\server\documents\paperport\jobs\jobs\!folder!" "\\mediaserver\archives\jobs\!folder!" /E /I /Y
echo(rd /S /Q "\\server\documents\paperport\jobs\jobs\!folder!"
echo(
set /A "amount+=1"
)
:: create the same amount of deleted chunk folders (if this is not needed just remove the following lines)
set /A "first=last+1"
set /A "last+=(amount*50)"
for /L %%a in (%first%, 50, %last%) do (
set /A "from=%%a", "to=%%a+49"
set "folder=!from!-!to!"
echo(md "\\server\documents\paperport\jobs\jobs\!folder!"
for /L %%b in (!from!, 1, !to!) do echo(md "\\server\documents\paperport\jobs\jobs\!folder!\%%b"
echo(
)
endlocal
penpen
Re: New to making batch files some help would be awesome.
EDIT1
This seems to work here in simple testing.
It only echos the robocopy commands to the screen.
If you are happy with it then backup your files and run a test after removing the echo statement and the first pause statement, and leave the last pause statement so you can see error messages if there are any.
The skip=10 is how many newest folders to leave, and it will move the rest.
The section after the first pause statement creates one set of folders, following in sequence from the latest folder.
This seems to work here in simple testing.
It only echos the robocopy commands to the screen.
If you are happy with it then backup your files and run a test after removing the echo statement and the first pause statement, and leave the last pause statement so you can see error messages if there are any.
The skip=10 is how many newest folders to leave, and it will move the rest.
The section after the first pause statement creates one set of folders, following in sequence from the latest folder.
Code: Select all
@echo off
pushd "\\server\documents\paperport\jobs\jobs" || goto :EOF
for /f "skip=10 delims=" %%a in ('dir /b /o-d /ad') do (
echo robocopy "%%a" "\\mediaserver\archives\jobs\%%a" /move /s
)
pause
for /f "tokens=2 delims=-" %%a in ('dir /b /od /ad') do set /a start=%%a + 1
set /a end=start + 49
for /L %%a in (%start%,1,%end%) do md "%start%-%end%\%%a"
popd
pause
Re: New to making batch files some help would be awesome.
Thank-you both very much for taking the time to help me.
Sorry for the delayed reply I was away camping!
The code because my level of understanding is very basic.
Thanks for commenting lines it is appreciated
I will look into both of your responses and get back to asap
thanks again
Craig
Sorry for the delayed reply I was away camping!
Just post, what is confusing you, and i may help.
The code because my level of understanding is very basic.
Thanks for commenting lines it is appreciated

I will look into both of your responses and get back to asap

thanks again
Craig

Re: New to making batch files some help would be awesome.
Re: New to making batch files some help would be awesome.
The skip=10 is how many newest folders to leave, and it will move the rest.
The section after the first pause statement creates one set of folders, following in sequence from the latest folder.
Code:
@echo off
pushd "\\server\documents\paperport\jobs\jobs" || goto :EOF
for /f "skip=10 delims=" %%a in ('dir /b /o-d /ad') do (
echo robocopy "%%a" "\\mediaserver\archives\jobs\%%a" /move /s
)
pause
for /f "tokens=2 delims=-" %%a in ('dir /b /od /ad') do set /a start=%%a + 1
set /a end=start + 49
for /L %%a in (%start%,1,%end%) do md "%start%-%end%\%%a"
popd
pause
Just tried this in a test environment and it looks promising, I didn't get any errors from cmd prompt but a couple of issues occurred.
Here is the edited code for my test (I'm not sure if because the dates are all now today's date that had an affect.)
Also not sure if people going back into the folders and modifying a job or file affects it as it doesn't seem to copy all files in folders.
Also no new folders were created.
The first time I ran the bat it said that each one already existed when it looked like it wanted to create more.
It looks like the script has either deleted the content of the coped files or re-created the files it moved (both scenarios are not ideal)
I have uploaded an image aswell to try and explain it more clearly .
What i would like to happen is once the copy has moved the oldest folders and all there contents the remaining 10 stay and 1 more new set added e.g
If the most recent file is 101-149 once the script is run it will create 150-199 with the sub folders inside.


Code: Select all
@echo off
pushd "\\server\documents\paperport\jobs\jobs\test" || goto :EOF
for /f "skip=10 delims=" %%a in ('dir /b /o-d /ad') do (
robocopy "%%a" "\\server\fileserver1\test\%%a" /move /s
)
for /f "tokens=2 delims=-" %%a in ('dir /b /od /ad') do set /a start=%%a + 1
set /a end=start + 49
for /L %%a in (%start%,1,%end%) do md "%start%-%end%\%%a"
popd
pause
Re: New to making batch files some help would be awesome.
The only thing you changed is the paths but you seem to have more folders and things in the working folder.
EDIT2: I have added a *_* filespec to each of the sections below.
I split the batch file into two for a test. Run this one and check if the folders and files are moved. Swap /s for /e if you have empty folders.
I can't tell easily from your screenshot what happened - sort your view by the date tab and you can see what is going to be moved more clearly.
Then test this - it will just echo the MD commands and pause after each one.
EDIT2: I have added a *_* filespec to each of the sections below.
I split the batch file into two for a test. Run this one and check if the folders and files are moved. Swap /s for /e if you have empty folders.
I can't tell easily from your screenshot what happened - sort your view by the date tab and you can see what is going to be moved more clearly.
Code: Select all
@echo off
pushd "\\server\documents\paperport\jobs\jobs\test" || goto :EOF
for /f "skip=10 delims=" %%a in ('dir *-* /b /o-d /ad') do (
robocopy "%%a" "\\server\fileserver1\test\%%a" /move /s
)
pause
Then test this - it will just echo the MD commands and pause after each one.
Code: Select all
@echo off
pushd "\\server\documents\paperport\jobs\jobs\test" || goto :EOF
for /f "tokens=2 delims=-" %%a in ('dir *-* /b /od /ad') do set /a start=%%a + 1
set /a end=start + 49
for /L %%a in (%start%,1,%end%) do echo md "%start%-%end%\%%a"& pause
popd
pause
Re: New to making batch files some help would be awesome.
Hey,
I think the problem I'm facing is that the jobs folders are accessed after they have been stored
for example.
We do a job for a builder but due to bad weather the job is put on hold, 2 months later the job is back on but he wants an adjustment / remake in which case the drawing is edited and saved in the same folder.
I think the problem I'm facing is that the jobs folders are accessed after they have been stored
for example.
We do a job for a builder but due to bad weather the job is put on hold, 2 months later the job is back on but he wants an adjustment / remake in which case the drawing is edited and saved in the same folder.
Re: New to making batch files some help would be awesome.
Running the edit you submitted will let you know the results 

Re: New to making batch files some help would be awesome.
Because your folders in that format also sort properly by numerics then try this instead, which no longer sorts by date.
It also has the filespec restricted to *-* to stop the extra folders from being included.
The extra pause command is there again to see if any errors occur.
It also has the filespec restricted to *-* to stop the extra folders from being included.
The extra pause command is there again to see if any errors occur.
Code: Select all
@echo off
pushd "\\server\documents\paperport\jobs\jobs" || goto :EOF
for /f "skip=10 delims=" %%a in ('dir *-* /b /o-n /ad') do (
robocopy "%%a" "\\mediaserver\archives\jobs\%%a" /move /e
)
pause
for /f "tokens=2 delims=-" %%a in ('dir *-* /b /on /ad') do set /a start=%%a + 1
set /a end=start + 49
for /L %%a in (%start%,1,%end%) do md "%start%-%end%\%%a"
popd
pause
Re: New to making batch files some help would be awesome.
Tested it with the same enviorement as before (no extra folders in the location)
And it appears to have worked!
With the new way you have coded the example what are the requirements for copy currently as i am struggling to understand some parts of the code
.
e.g the below
And it appears to have worked!

With the new way you have coded the example what are the requirements for copy currently as i am struggling to understand some parts of the code

e.g the below
Code: Select all
|| goto :EOF
for /f "skip=10 delims=" %%a in ('dir *-* /b /o-n /ad') do (
Re: New to making batch files some help would be awesome.
It's good to hear that you had some success.
|| goto :EOF will skip the batch file if the server is offline - it will only run if the pushd command completed successfully.
&& is an operator that will execute on errorlevel 0 and || will execute on errorlevel 1
With folders in this format, they all sort as shown below when sorted by name.
So the code now uses sorting by name/reverse-name and it skips the 10 highest numbered folders.
The *-* filters only processes folder names with a hyphen.
With a reverse sort by name the highest numbered folders are at the top.
24400-24449
24450-24499
24500-24549
24550-24599
24600-24649
24650-24699
24700-24749
24750-24799
24800-24849
24850-24899
24900-24949
24950-24999
25000-25049
25050-25099
25100-25149
|| goto :EOF will skip the batch file if the server is offline - it will only run if the pushd command completed successfully.
&& is an operator that will execute on errorlevel 0 and || will execute on errorlevel 1
With folders in this format, they all sort as shown below when sorted by name.
So the code now uses sorting by name/reverse-name and it skips the 10 highest numbered folders.
The *-* filters only processes folder names with a hyphen.
With a reverse sort by name the highest numbered folders are at the top.
24400-24449
24450-24499
24500-24549
24550-24599
24600-24649
24650-24699
24700-24749
24750-24799
24800-24849
24850-24899
24900-24949
24950-24999
25000-25049
25050-25099
25100-25149
Re: New to making batch files some help would be awesome.
Thanks for the explanation!
One more question if that's ok!
How hard would it be to create 3 folders inside all of the individual jobs?
The folders will always be named the same e.g
100-149 > 100 > cads
production
purchasing
101 > cads
production
purchasing
102 > cads
production
purchasing
One more question if that's ok!
How hard would it be to create 3 folders inside all of the individual jobs?
The folders will always be named the same e.g
100-149 > 100 > cads
production
purchasing
101 > cads
production
purchasing
102 > cads
production
purchasing
Re: New to making batch files some help would be awesome.
Replace for /L %%a in (%start%,1,%end%) do md "%start%-%end%\%%a" with this and test it:
Code: Select all
for /L %%a in (%start%,1,%end%) do (
md "%start%-%end%\%%a"
md "%start%-%end%\%%a\cads"
md "%start%-%end%\%%a\production"
md "%start%-%end%\%%a\purchasing"
)