Directory Equals Filename BEFORE Underscore

February 23, 2011 at 11:28:11
Specs: Windows XP
I have taken the below code from another thread and it works perfectly provided the directory I want to create is only 6 characters long.

Now the problem I have is some of the files I am moving have 6, 7 or 8 characters BEFORE the underscore.

Basically I need to know how to change the provided code to dynamically create a folder based on the characters before the underscore.

eg:
file name "666666_1.txt" creates a folder and is moved to folder "666666"
file name "7777777_1.txt" creates a folder and is moved to folder "7777777"
file name "88888888_1.txt" creates a folder and is moved to folder "88888888"

Any clue on how to help?!

Code for 6 characters:

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d *.txt') do (
set F=%%a
set F=!F:~0,6!
if not exist !F! md !F!
move "%%a" !F!
)


See More: Directory Equals Filename BEFORE Underscore

Report •


#1
February 23, 2011 at 13:54:53
im sure that the part:
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d *.txt') do (
set F=%%a
set F=!F:~0,6!
if not exist !F! md !F!
move "%%a" !F!
)

should be

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d *.txt') do (
set F=%%a
set F=!F:~0,8!
if not exist !F! md !F!
move "%%a" !F!
)
this basically tells the bat that it should give space for 8 characters and not 6
now it counts from 0 to 8 !F:~0,8! instead of 0 to 6 !F:~0,6!

D_Pikster


Report •

#2
February 23, 2011 at 13:59:31
I think I may have figured it out...I also changed some variable names for clarity.

@echo off & setlocal EnableDelayedExpansion

for /f "tokens=1* delims=_" %%a in ('dir /b *.txt') do (
if "%%b" neq "" (
set NewDir=%%a
set NewDir=!NewDir!
if not exist !NewDir! md !NewDir! 2>nul
move "%%a_%%b" "!NewDir!\"
)
)


Report •

#3
February 23, 2011 at 22:45:01
yes you can do it this way but then you wont define that number of chars the name will be.
do you want it like that? here you just tell it to have a name with a _ in there

Report •

Related Solutions


Ask Question