Register
It is currently Sat Sep 20, 2014 4:01 pm

Archive Files


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Sep 24, 2009 8:50 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 11:50 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 547
most of all what's above is not bash !?

If you want to make a .bat file, then learn basic, or whatever powershell provides...

If you want to make a bash script, I suggest you first read :
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
and of course :
http://www.gnu.org/software/bash/bash.html


Top
 Profile  
 PostPosted: Thu Sep 24, 2009 8:28 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 547
Ok, ok, I'm going to tell you how I would do it
Code:
#!/bin/bash

# first, no space around = to define a variable
# second, no space in file names
#path="C:\Document_QC\"
# as I don't have C:/, I'll use /home/$USER, or ~, it's even

path="~"
targetDirectory="Input-Delete"

# if a variable is immediately followed by a character, best is to put its name in curly braces
# don't use variable in ' ', it won't be interpreted as a variable, but as any string

outputDirectory="${targetDirectory}-Archive"


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

# how deep do you want to look for files
# this will not respect hierarchy => all files found will go into the same directory

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

This might not do exactly what you want to do, so you'll have to tell us, as clearly as possible, what isn't going your way.


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

Joined: Thu Sep 24, 2009 8:41 am
Posts: 5
First of all, thank you very much for the reply. I modified the code to hit the directories I need. 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 10:52 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 547
Are you using the script directly in cmd.exe (where it shouldn't work), or do you use cygwin (or any other *N*X shell emulator)?


Top
 Profile  
 PostPosted: Fri Sep 25, 2009 11:20 am   

Joined: Thu Sep 24, 2009 8:41 am
Posts: 5
The results are from a run in Cygwin. I am setting this up as a scheduled task in Windows 2003 though, is that going to be an issue?


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


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