Batch script to separate log files by name

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Batch script to separate log files by name

Post by MigrationUser »

17 Apr 2007 11:53
joinup


Hi there smile
I have made a batch script to move log files from a server to a specific location, ok it´s done.
But now i want to, separate the logs files by name, and put them into a folder with the name of the file, if doesnt exist a folder with that name, batch make a mkdir and create a directory, and put all files with that name into that folder, and so on.
I supode that would be with For command, but i need help to load the file name into a variable, the name of logs files r like this, something.log-access.something, o wanna read the name just until the first . , tanks 4 your time.
Please respond roll

Farewell

----------------------------

#2 17 Apr 2007 14:47
bluesxman


How's this?

Code: Select all

@echo off

set sourcedir=Z:\somedir
set files=*.log

for %%F in ("%sourcedir%\%files%") do (
    for /f "tokens=1 delims=." %%D in ("%%~nxF") do (
        mkdir "%sourcedir%\%%D" 2>nul
        move /y "%%~F" "%sourcedir%\%%D"
    )
)
cmd | *sh | ruby | chef

----------------------------

#3 17 Apr 2007 15:20
joinup


Tkx a lot is allready working, next time i get any problem i will post here smile
Post Reply