Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


batch command to move file based on criteria in file name

Posted on 2015-01-07
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 25

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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 85

Accepted Solution

oBdA earned 2000 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 25

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
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…

715 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