Batch script to move files older than 1 day

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Megsie
Posts: 13
Joined: 05 May 2016 11:06

Batch script to move files older than 1 day

#1 Post by Megsie » 26 May 2016 14:37

Hello all,

I've been trying to modify a windows batch script to move .txt files that are older than one day into a different folder, but have been unsuccessful. Could someone please help?

Here's what I've written so far:

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd -c move @file U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"

It doesn't give me any errors, but it also doesn't move the files.... Any ideas?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to move files older than 1 day

#2 Post by Squashman » 26 May 2016 15:22

cmd.exe use slashes for its options.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Batch script to move files older than 1 day

#3 Post by Megsie » 26 May 2016 15:32

My delete script without slashes works fine, not sure if this is based on versions...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -s -m *.txt* /D -2 /c "cmd /c del @path"

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Batch script to move files older than 1 day

#4 Post by Megsie » 26 May 2016 15:36

When I run my move script it returns

"Microsoft Windows [Version 6.1.7601]
Copyright (C) 2009 Microsoft Corporation. All rights reserved."

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to move files older than 1 day

#5 Post by Squashman » 26 May 2016 17:06

Megsie wrote:When I run my move script it returns

"Microsoft Windows [Version 6.1.7601]
Copyright (C) 2009 Microsoft Corporation. All rights reserved."

Because you are using cmd -c. I told you that cmd.exe uses slashes just like the 2nd example your delete uses.

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Batch script to move files older than 1 day

#6 Post by Megsie » 27 May 2016 16:22

Ok, I've changed my code as follows:

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"

But now it's telling me "The syntax of the command is incorrect."

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch script to move files older than 1 day

#7 Post by Squashman » 27 May 2016 22:19

And what is the rules about file names or paths with spaces in them?

Megsie
Posts: 13
Joined: 05 May 2016 11:06

Re: Batch script to move files older than 1 day

#8 Post by Megsie » 30 May 2016 11:38

I'm not a programmer or script writer, I don't know the rules.

I've tried putting underscores instead of spaces and adding extra quotations, but the script doesn't work...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\""


...hence I came to a tip forum for help.

elias
Posts: 5
Joined: 19 May 2016 18:20

Re: Batch script to move files older than 1 day

#9 Post by elias » 30 May 2016 18:51

One time is instead of directly doing "cmd /c move @file", I would do a "cmd /c echo move ....".

Simply add "ECHO" to see the command that will be executed. This will let you troubleshoot what's the problem and adjust the command accordingly.

You may want to resort to using "FOR /F" with the "usebackq" switch as a secondary step.

Check the examples here https://github.com/PassingTheKnowledge/Batchography/ especially the "*token*.bat" examples.

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: Batch script to move files older than 1 day

#10 Post by douglas.swehla » 01 Jun 2016 14:25

Megsie wrote:I'm not a programmer or script writer, I don't know the rules.

If you're going to be doing any of this, it's worth learning the rules, or at least the basics. Here is a nice overview on quotes and other special characters: http://ss64.com/nt/syntax-esc.html

Megsie wrote:I've tried putting underscores instead of spaces ...

If a file is named "My File.txt", and you refer to it as "My_File.txt", that will not work, because the file "My_File.txt" does not exist.
If you rename "My File.txt" to "My_File.txt", then you can refer to it with or without quotes ("My_File.txt", My_File.txt), and it will be fine either way.

Megsie wrote:I've tried . . . adding extra quotations, but the script doesn't work...

Code: Select all

forfiles -p "U:\My Documents\1. RPC\AutoMed Archive" -m *.txt -d -1 /c "cmd /c move @file "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\""

Your quoting looks okay to me. You've quoted the source and destination directory paths to handle spaces, and you've quoted the entire command string, as required by the FORFILES command.

I think the problem is with how you're referring to the file to be moved in the MOVE command.
  • The @file variable returns only the name and extension of the file, e.g. "MyFile.txt".
  • The @path variable returns the full path to the file, e.g., "U:\My Documents\1. RPC\AutoMed Archive\MyFile.txt".
If the batch file is not located in the same folder as the files, the MOVE command will be trying to act on local files that don't exist:

Code: Select all

move C:\path\to\batchfolder\"MyFile.txt" "U:\My Documents\1. RPC\AutoMed Archive\Problem Files\"
rem The command fails, because file C:\path\to\batchfolder\"MyFile.txt" does not exist.


Both variables return an already-quoted string, so you needn't requote them: @path is fine, "@path" is overkill.
Replace @file with @path, test it using the ECHO command as elias suggested, and let us know how it goes.

Post Reply