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 24

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.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 84

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 24

Expert Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

830 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