tags:

views:

1403

answers:

5

I want to write a DOS batch process which will go through my Directory and move all *.txt files to a dest folder to starting with the first character of the txt files

Ex.
abc.txt will move to folder "a"
def.txt will move to folder "d"
and so on...

A: 
copy sourcepath\a*.txt destinationpath\a

copy sourcepath\b*.txt destinationpath\b

klabranche
I need an automated script which goes through the directory and does this exact thing by itself :)
Murtaza RC
+2  A: 

From the command line:

for %i in (*.txt) do (set FOLDER=%i & move %i %FOLDER:~0,1%)

In a batch file, you would have to double the %'s, like this:

for %%i in (*.txt) do (set FOLDER=%%i & move %%i %FOLDER:~0,1%)
lavinio
Jay
A: 

I think the point of the problem is to extract the first letter.

you can use the ~ char to extract the first letter of the file name

set str=filename
echo %str:~0,1%
f
Jonathan
A: 

You can use the biterscripting chex command (character extractor) to extract the first character of the file name. Here is the script. Assume your files are at C:\My Directory.

# Script move1.txt
# Change directory to My Directory.
cd "C:\My Directory"

# Collect a list of *.txt files.
var str list ; lf -n "*.txt" > $list

# Go thru files one by one.
while ($list <> "")
do

    # Get the next file.
    var str file ; lex "1" $list > $file

    # $file now has full path of a found *.txt file. Get just the file name.
    var str filename ; stex -p "^/^l[" $file > $filename

    # $filename has just the file name. Get the first character.
    var str firstchar ; chex -p "1" $filename > $firstchar

    # $firstchar now has the first char of file name. The folder where we
    # want to move this file is, then, C:\Destimation Folder\$firstchar .
    var str destfolder ; set $destfolder = "C:\Destination Folder\"+$firstchar

    # Move $file to $destfolder.
    system move ("\""+$file+"\"") ("\""+$destfolder+"\"")

done

Script is in biterscripting ( http://www.biterscripting.com ) . To try, save the script as C:\Scripts\move1.txt, start biterscripting, enter the following command.

script move1.txt

If you need to run the script periodically, schedule the following command in the task scheduler.

"C:\biterScripting\biterScripting.exe" "move1.txt"

I have not tested the script, test it on sample files. Make sure you change the "C:\My Directory" and "C:\Destination Folder" to their correct values. Always use double quotes with file names and paths.

P M
A: 

move Office XL File by using DOS