Register
It is currently Fri Dec 19, 2014 12:44 am

Create Directory and archive files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Sep 24, 2009 9:12 am   

Joined: Thu Sep 24, 2009 8:41 am
Posts: 5
I'm trying to create a scheduled task that will move files over 30 days old to an archive directory. I'm going to create a Windows .bat file, but don't know Bash well enough (and couldn't find the answer anywhere else), and run it as a nightly scheduled task.

1. Search for '$targetDirectory - Archive'
a. if it does not exist, create it
b. if it does exist, do nothing
2. Move all files over 30 days old to this Archive Directory

This should be simple enough, but I'm having trouble getting it to work. Below is the code I wrote so far:

---------------------------- code ---------------------------------------------------------------------

path = "C:\Document QC\"
targetDirectory = "Input-Delete"
outputDirectory = "'$targetDirectory' - Archive"

createOutputDirectory = 0

foreach directory in $path
do
if [ $directory -ne $outputDirectory ]; then
$createOutputDirectory = 1
done

if [ $createOutputDirectory -eq 1 ]; then
mkdir $path$outputDirectory
done

forfiles -P "$path$targetDirectory" -S -M *.* -D -30 -C "cmd /C mv @FILE $path$outputDirectory"

---------------------------- code ---------------------------------------------------------------------

Thanks for the help.

-Jared


Top
 Profile  
 PostPosted: Thu Sep 24, 2009 5:42 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
You need to watch out for a few things:
- Don't put spaces around the '=' in variable assignments
- When you have paths that could have spaces in them, surround them in double-quotes every time you use them
- Use 'fi' to end your if blocks

Here's a cleaner and (hopefully) working version of the the beginning of your code. The single -d test can replace the looping you were doing.
Code:
path="C:\Document QC\"
targetDirectory="Input-Delete"
outputDirectory="$targetDirectory - Archive"

# Create the directory if it doesn't exist yet
if [ ! -d "$path$outputDirectory" ]; then
  mkdir "$path$outputDirectory"
fi

# Don't know what this does...
forfiles -P "$path$targetDirectory" -S -M *.* -D -30 -C "cmd /C mv @FILE $path$outputDirectory"


I don't know what your last statement is trying to do. Are you trying to move a certain set of files from "$path$targetDirectory" to "$path$outputDirectory"? If you only want to move some of the files based on some test, you should probably use find (check out the very long man page on it), specifically with -exec.

If you'll explain that last bit, I could help you out with it.


Top
 Profile  
 PostPosted: Fri Sep 25, 2009 9:52 am   

Joined: Thu Sep 24, 2009 8:41 am
Posts: 5
First of all, thank you very much for the reply. I modified my code quite a bit based on responses from other threads. Below is what I have so far, but it's still not working. I tried executing in both XP, and Server 2003, and both are erroring out. I'm listing the full modified code (I tried forward and backward slashes in every combination I could think of, and nothing seems to work), as well as the errors I'm seeing. What results is a directory called $path with a subdirectory $outputDirectory that appears in the same folder the script was run in.

----------------------------------------- code -----------------------------------------------------------

#!\bin\bash

path="C:\DocumentQC\"
targetDirectory="Input-Delete"
outputDirectory="${targetDirectory}-Archive"

find $path -maxdepth 1 -type d -iname $outputDirectory || mkdir $path\$outputDirectory

find $path\$targetDirectory -atime +30 -type f -exec mv {} $path\$outputDirectory\;

----------------------------------------- code -----------------------------------------------------------
--------------------------------- execution and error --------------------------------------------------

$ ./Script.bat

c:\Shared files>#!\bin\bash
The system cannot find the path specified.

c:\Shared files>path=C:\DocumentQC\"

c:\Shared files>targetDirectory=Input-Delete"
'targetDirectory' is not recognized as an internal or external command, operable program or batch file.

c:\Shared files>outputDirectory=${targetDirectory}-Archive"
'outputDirectory' is not recognized as an internal or external command, operable program or batch file.

c:\Shared files>find $path - maxdepth 1 -type d -iname $outputDirectory || mkdir $path\$outputDirectory
'find' is not recognized as an internal or external command, operable program or batch file.

c:\Shared files>find $path\$targetDirectory -atime +30 -type f -exec mv {} $path\$outputDirectory \;
'find' is not recognized as an internal or external command, operable program or batch file.

--------------------------------- execution and error --------------------------------------------------


Top
 Profile  
 PostPosted: Fri Sep 25, 2009 12:26 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
First of all, you cannot run bash scripts in windows batch script engine.
It is not the same thing.

Another thing is that the shabang you've added (#!\bin\bash) is not of any use... in linux it tells the shell which program to execute the file with (if run with ./), in windows you cannot make your file executable like that so the shabang is not needed.

Also, "find" utility is a linux utility... there are ports of bash to Windows which includes alot of the common utilitys or you can use CygWin.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Sep 25, 2009 6:41 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Yeah, I assumed you were using cygwin here. I honestly don't know how paths work in Windows with that whole C:\ thing, so it still might not work under cygwin.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP