Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Batch File to Identify, Create Folder, and Move file

$
0
0

Greetings,

I've spent the last day trying to find a way to create a batch file that would do the following;

I have a folder that one of our pieces of software dumps .dat files into.  I'm trying to make a batch file that I can set up with a Windows Task Scheduler that would run once a day or whenever and clean up this folder.

I'm wanting to place the files in the appropriate Month and year.

I found a script that sorta does what I want but I can't figure out how to get it to identify the right file names.

Example:

The naming conventions of the files are as follows

HOURLY21JUL2020.DAT
ALTIMUS_BILL21JUL2020_1730_188031.DAT
Bill_ALTIMUS_BILL21JUL2020_1730_188031.DAT
EV_ALTIMUS_BILL21JUL2020_1730_188031.DAT

I'm trying to create a batch file that would identify, at least, the Month and Year and create a File 2020 > JUL and move the files to those folders.  If the folder already exists then it would just move the file.

I found the following code on another site but I couldn't get it to do what I was wanting because there are different amounts of characters in the name. 

I have about 70k files I need to clean up in the folder and figured if I get a working batch file it wouldn't take that long to clean it up and have the folders created and then put it with Windows Task and it run on it's on daily or whenever. 

@echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.dat') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,1!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!" >nul
endlocal
)


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>