# batch file



## bbright20 (Sep 19, 2007)

Is it possible to write a single bat file that will perform the following tasks:

Move data from a folder with structure like "FOLDER NAME" then "DATE (MMDD format)"
to another folder? The date will always change (example 0919 today, then 0920 tomorrow), but I want this process to run every day (with the new folder (date) name).

Also, it is possible to do this as well:
Move data from a folder to another folder, then rename the files in the destination folder?


----------



## JohnWill (Oct 19, 2002)

I'd use a VB script or something like AutoIt to do the trick, principally because I'm lazy and figuring out that date in a batch file looks like a PITA.


----------



## bbright20 (Sep 19, 2007)

any suggestions on where to begin? I've never used that program before.


----------



## ghostdog74 (Dec 7, 2005)

there's no need to use AutoIT. you are not that lazy yet right? i am sure you want to learn something. So fire up your command prompt, and type move /? , or xcopy /? or copy /? and experiment with them.


----------



## bbright20 (Sep 19, 2007)

tried most of those commands...the problem is that the "date" folder will change daily. I want a single batch file that I don't have to edit every day.



any suggestions on the other batch file? Moving files from one folder to another then renaming them in the destination folder?


----------



## ghostdog74 (Dec 7, 2005)

since you want the process to run everyday, then everyday you will only have one file in the directory ? you can juse use the wildcard then?
if not, then you should elaborate more .


----------



## bbright20 (Sep 19, 2007)

I have a series of PDF files that are dropped into a date folder that I want to move to a destination folder.

example: C:\pdf\0920\*.pdf
the 0920 folder will change daily.

I would like the files deposited to (example) c:\worked\

This is something that would run daily, so the 0920 folder will need to be changed daily. I'm trying to create a batch file that will automatically adjust the name of this folder within the batch file to reflect whatever day it is (in mmdd format).

example:
(If run today) copy c:\pdf\0920\*.pdf c:\worked\ 
(If run tomorrow - same batch file with no modifications from me) 
copy c:\pdf\0921\*.pdf c:\worked\
the date folder will always be different, but I don't want to have to adjust the batch file everyday to reflect the change in date.


----------



## ghostdog74 (Dec 7, 2005)

sorry, i don't usually code in batch, so here's a vbscript

```
Set objFSO = CreateObject("Scripting.FileSystemObject")
myFolder = "c:\temp" 'source folder
destFolder = "c:\test1" 'destination folder
theDay = Day(Now)
If Len(theDay) < 2 Then
	theDay = "0"&theDay 'put 0 if single digit day
End If 
theMonth = Month(Now)
If Len(theMonth) < 2 Then
	theMonth = "0"&theMonth 'put 0 if single digit month
End If 
theDir = theMonth & theDay
myFolder=myFolder&"\"&theDir 'define pdf folder
If Not objFSO.FolderExists(myFolder) Then
	WScript.Echo "Folder " , myFolder ,"does not exists"
	WScript.Quit(1)
End If 
For Each myFiles In objFSO.GetFolder(myFolder).Files
	If objFSO.GetExtensionName(myFiles) = "pdf" Then
		objFSO.MoveFile myFiles , destFolder&"\"&myFiles.Name 
	End If 
Next
```
if you want to do in batch, use the date command , check out 

```
date /T
```
and the for loop , for /? for information. the for loop is used to get the day and month of the date /T output.


----------



## bbright20 (Sep 19, 2007)

ghostdog - you rock. That totally worked.

Thank you SOOOO much!

Is it possible to create the second batch file I was asking about?

I need to move files from source directory to destination, then have them renamed in the destination folder. I would like this in one batch file - currently I have 2 seperate batch files - 1 to move the files, and 1 to rename the files once they are moved.

THANKS AGAIN!!!


----------



## ghostdog74 (Dec 7, 2005)

bbright20 said:


> I need to move files from source directory to destination, then have them renamed in the destination folder. I would like this in one batch file - currently I have 2 seperate batch files - 1 to move the files, and 1 to rename the files once they are moved.
> 
> THANKS AGAIN!!!


no need to. In the vbscript, you just give it a different name..

```
....
objFSO.MoveFile myFiles , destFolder&"\"&"anothername.pdf" 
....
```
sorry, no deal with batch. you have to wait for someone else..or try to do batch yourself.


----------



## bbright20 (Sep 19, 2007)

ok, i'll give that a try.

I'm new to vbs, so please bear with me. You seem like you really know what you are doing, so I'll go ahead and ask.

Here's my situation:

I have a directory (example: C:\pdf\)
in this folder, there are several other folders (example C:\pdf\error1\ C:\pdf\error2, etc.)

Within these folders are files (pdf files with renamed extensions to show me they failed - *.failpdf).

Is it possible to create a script that will check all folders within the C:\pdf\ folder (error1, error2, etc - starting with the first error1 folder) for files with that extension (*.failpdf), then move them to the next consecutive error folder (error1 *.pdf files move to error2 folder, then renames to *.pdf)? Once the process is completed, I would need to re-run the script to go to the next consecutive folder (error1 files were moved to error2, then check error2 files for *.failpdf and move them to error3 folder, then rename to *.pdf).

Sorry if I'm asking too much...I'm just learning this language, so I'm not used to it.

Thanks again for all your help!


----------



## ghostdog74 (Dec 7, 2005)

bbright20 said:


> (error1 *.pdf files move to error2 folder, then renames to *.pdf)? Once the process is completed, I would need to re-run the script to go to the next consecutive folder (error1 files were moved to error2, then check error2 files for *.failpdf and move them to error3 folder, then rename to *.pdf).


wow, a different requirement. should have posted together with the first post.
anyway. I really don't understand this part..what exactly are you trying to achieve in the end?


----------



## bbright20 (Sep 19, 2007)

ghostdog74 said:


> wow, a different requirement. should have posted together with the first post.
> anyway. I really don't understand this part..what exactly are you trying to achieve in the end?


In the end, I'm trying to get all files with a *.failpdf extension moved to multiple folders, then renamed with *.pdf.

I have a process that imports pdf files into a system, but ones that don't import are tagged with the *.failpdf extension. Each processing folder would hold these *.failpdf files. I would want these moved to the next consecutively named folder - then renamed *.pdf so they would process in the next folder as well.

Structure looks like this:

C:\pdf\error1\*.failpdf (could be multiple files)
I want to move these *.failpdf files to C:\pdf\error2\*.pdf, then rename them to *.pdf so they will process.
After they are renamed and processed, I will end up with more failed files (*.failpdf), so I would want to move them from C:\pdf\error2\ to c:\pdf\error3\
And so on until I am out of processing files. I'm hoping there is a way to simply look in the directory C:\pdf\error(*) for these *.failpdf files, then move them to the next folder C:\pdf\error(*+1)\.

In other words, if it finds *.fail pdf files in error2 folder, move from error2 folder to error3 folder, then rename the *.failpdf files to *.pdf.

Maybe something that will look in error1 - if there are *.failpdf files there, move to error2...but have the coding say look in error1, then error2, then error3, etc. It would need to move from error1 to error2, or from error2 to error3, etc. IF there are no *.failpdf files in error1, move on to error2, etc.

There is the potential for up to 7 or 8 of these error folders (error1, error2, error3)... Each time the process is run, I end up with failed pdf files (*.failpdf).

Is that a better explanation?


----------



## ghostdog74 (Dec 7, 2005)

i will do this favor however, please try to read up (and practice!) more on vbscript, if you are really interested. Its the next best thing to DOS, considering its nativity to the Windows OS.

```
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim ErrFolders(),i
i=0
myFolder = "c:\temp" 'source folder
If Not objFSO.FolderExists(myFolder) Then
	WScript.Echo "Folder " , myFolder ,"does not exists"
	WScript.Quit(1)
End If 
For Each myFolders In objFSO.GetFolder(myFolder).SubFolders
	If Left(myFolders.Name,5) ="error" Then
		ReDim Preserve ErrFolders(i) 
		ErrFolders(i) = myFolders.Path
		i=i+1
	End If 
Next 
On Error Resume Next
For j=LBound(ErrFolders) To UBound(ErrFolders)
	errDir = ErrFolders(j)
	For Each myFiles In objFSO.GetFolder(errDir).Files
		If objFSO.GetExtensionName(myFiles) = "failpdf" Then
		    FileName = objFSO.GetBaseName(myFiles)		   
		    objFSO.MoveFile myFiles , ErrFolders(j+1)&"\"&FileName&".pdf" 		
		End If 
	Next		
Next
```
Note that the last error folder's failpdf will not be moved, since there is no more error folders left.


----------



## bbright20 (Sep 19, 2007)

Thanks for all your help.

I couldn't get that last script to run properly.

If it helps, the source folder is V:\ and the subfolders will be Wake1, Wake2, Wake3, etc.

I ran the script, then watched the folder, but the *.failpdf files stayed in the same folder.


----------



## ghostdog74 (Dec 7, 2005)

bbright20 said:


> Thanks for all your help.
> 
> I couldn't get that last script to run properly.
> 
> ...


no it does not help me at all. show your code. I don't want to guess what you did..


----------



## bbright20 (Sep 19, 2007)

ghostdog74 said:


> no it does not help me at all. show your code. I don't want to guess what you did..




```
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim ErrFolders(),i
i=0
myFolder = "V:\" 'source folder
If Not objFSO.FolderExists(myFolder) Then
	WScript.Echo "Folder " , myFolder ,"does not exists"
	WScript.Quit(1)
End If 
For Each myFolders In objFSO.GetFolder(myFolder).SubFolders
	If Left(myFolders.Name,5) ="Wake" Then
		ReDim Preserve ErrFolders(i) 
		ErrFolders(i) = myFolders.Path
		i=i+1
	End If 
Next 
On Error Resume Next
For j=LBound(ErrFolders) To UBound(ErrFolders)
	errDir = ErrFolders(j)
	For Each myFiles In objFSO.GetFolder(errDir).Files
		If objFSO.GetExtensionName(myFiles) = "FAILEDPDF" Then
		    FileName = objFSO.GetBaseName(myFiles)		   
		    objFSO.MoveFile myFiles , ErrFolders(j+1)&"\"&FileName&".pdf" 		
		End If 
	Next		
Next
```


----------



## ghostdog74 (Dec 7, 2005)

This code:

```
...
If Left(myFolders.Name,5) ="Wake" Then
...
```
means starting from the left of the string till the 5th character, check whether its equal to "Wake". Since you have once again changed your requirements, your subfolders are now Wake1, Wake2, etc instead of error1, error2 etc, do you think the above would match? how many characters is "Wake"? Try to amend this, run it again and see if it works


----------



## bbright20 (Sep 19, 2007)

That worked!

I don't mean to keep changing the requirements...there are others involved in this process that are changing the folders as well. I thought it be best to give a generic example, then tailor it to our needs.

Had I been more fluent in the vbscript writing, I would've caught the "left" problem. As I stated before, I am just now learning all of this scripting.

I would like to thank you again for all of your help with these scripts. You have made my life 100% easier.

On another note, can you recommend any books or sites where I can begin learning vbscript? I have some knowledge of coding, but very basic. I've seen a couple books on another scripting help site, but I wasn't sure about them. I've also seen Microsoft's internet help for vbscript, but was looking for a more "beginner" approach.

Anyway, thank you again for all of your help. I really do appreciate it. I also appreciate your effort to get me to learn this code as well, rather than simply telling me the answers. When I posted my last problem, you very well could've simply told me what the problem was and what the solution was, but instead you directed me to where the problem was and offered suggestions on how to fix it. This is a great way for me to learn, and again, I really appreciate it.


----------



## ghostdog74 (Dec 7, 2005)

don't mention it. glad it worked for you. You can download this help from microsoft


----------



## bbright20 (Sep 19, 2007)

hate to bother everyone again, but I'm having trouble creating a script that goes along with this one...

Here's what I want to do....I want to rename a folder in the "myfolder" directory (myfolder=myfolder&"/"&thedir) to have a "D" at the end of it...in other words, 1105D.
I then want to move this folder to the destfolder (2007 Done).

This is what my directory structure looks like:

my folder I want renamed and moved (using today's date as an example 1105):
H:\Ermx Processing\Alexander\1105

destination folder:
H:\Ermx Processing\Alexander\2007 Done

Here's what i have so far.


```
Set objFSO = CreateObject("Scripting.FileSystemObject")
myFolder = "H:\Ermx Processing\Alexander" 'source folder
destFolder = "H:\Ermx Processing\Alexander\2007 DONE" 'destination folder
theDay = Day(Now)
If Len(theDay) < 2 Then
	theDay = "0"&theDay 'put 0 if single digit day
End If 
theMonth = Month(Now)
If Len(theMonth) < 2 Then
	theMonth = "0"&theMonth 'put 0 if single digit month
End If 
theDir = theMonth & theDay
myFolder=myFolder&"\"&theDir 'define pdf folder
If Not objFSO.FolderExists(myFolder) Then
	WScript.Echo "Folder " , myFolder ,"does not exists"
	WScript.Quit(1)
End If 

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder myfolder ,  myfolder&"D"
```
I can get the file renamed, but when I try a code to move myfolder to the destfolder like:


```
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder myfolder ,  destFolder
```
It gives me an error. Am I supposed to redefine the "myfolder" variable? I've been searching on this, but I can't seem to find a code that will work.

EDIT - Also, is it possible to simply skip a file if it's not there? I have several folders I want to apply this to, but some may or may not have the 1105 (date) folder in it. I can adjust the code to reflect the other folders... I'm trying to have it read a series of folders and look for the 1105 (date) folder, if there's one there, rename it and move it to the 2007 Done folder but all done in one vbs file.

Example: No 1105 folder in Alexander, but one in Cumberland (other folder not defined in code above - I will adjust this later).

Thanks in advance.


----------



## bbright20 (Sep 19, 2007)

anyone able to help out?????


----------

