batch command to move file based on criteria in file name

Posted on 2015-01-07
Last Modified: 2015-01-07
I have a need to move a file from one folder to next based on criteria in a file name. When a file is imported, a date and timestamp is appended to the file like this: 20150107-0853_daxwms. If the same file is imported again it will look like this
20150107-1204_20150107-0853_daxwms. So what i am trying to do is only move the file if the date and timestamp only exist once.

The file name will always have "daxwms" after the Date/Timestamp. So since an underscore separates the date/timestamp i was thinking the best way is "if 2 underscores exist before daxwms then don't import." keeping in mind that underscores may exist after the daxwms.  

Thanks for any help you can give.
Question by:jacobJL
  • 3
  • 2
LVL 23

Expert Comment

ID: 40536171
Do you have a .bat file doing this?
This would stamp it if the filename pattern DOES NOT exist:
if not exist ????????-????_daxwms (
  command 1
  command 2

Open in new window


Author Comment

ID: 40536333
Yes i want this in a bat file.
But i don't think this will work because ????????-????_daxwms will always exist. The problem is when the file is reimported the date is appended again.

What i don't want to do is import the file more than twice. So i am looking to only move the file if the date is appended only once. Does this make sense?

Author Comment

ID: 40536338
let me explain how our imports work.
The file goes into an input folder, then once processed goes either to a archive folder or dump folder. If it goes to the dump folder i want to reimport the file into the input folder. If it dumps again i no longer want to import it.
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 83

Accepted Solution

oBdA earned 500 total points
ID: 40536354
How about this? It's in test mode and will only display the move command it would normally run; remove the uppercase ECHO in line 7 to run it for real:
@echo off
set SourceFolder=C:\Dump
set TargetFolder=D:\Input
set FileMatch="^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]_daxwms.*$"
for /f "delims=" %%a in ('dir /a:-d /b "%SourceFolder%\*.*" ^| findstr.exe /i /r %FileMatch%') do (
	ECHO move "%SourceFolder%\%%a" "%TargetFolder%"

Open in new window

Edit: note that the ".*" at the end of the FileMatch variable is not an extension wildcard; the whole string is a regular expression, and the ".*" will "eat" the complete rest of the file name after the date and the daxwms.

Author Closing Comment

ID: 40536462
Thank you this worked great! I had a feeling regex would be the best solution here. and thanks for the extra note, that's definitely something i will keep in mind for future use.

NewVillageIt, thank you as well. I just couldn't get yours to work right.
LVL 23

Expert Comment

ID: 40536466
No worries, mate. Have a good one.

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now