0
Thanks

A few words of thanks would be greatly appreciated.

Rename file based on data in file

Rename file based on data in file

Issue

I'm hoping someone can help me create a batch file to rename text files in a specific folder to include data from the file. It is an asterisk delimited file and I need to use the data from the 12th field.

Solution

  • Here is a batch script.
# Script Rename12.txt 
var string folder 
var string list, file, content, field12 
if ($folder=="") 
    exit 1 "Error 1: folder not specified." 
endif 
cd $folder 

# Collect a list of files 
lf -n "*" > $list 

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

    # Get the next file 
    lex "1" $list > $file 

    # Get the file's contents into a string variable. 
    cat $file > $content 

    # Get the value after 11th asterisk and before the next asterisk. 
    stex "^*^11[" $content > $field12 ; stex "[^*^" $field12 > null 

    # Rename file to the 12 field. 
    system rename ("\""+$file+"\"") ("\"file_"+$field12+"\"") 
done 
    • Script is in biter scripting. Save the script in file C:/Scripts/Rename12.txt, run with this command in biter scripting.
script "C:/Scripts/Rename12.txt" folder("C:/test")
    • It will rename all files in folder C:/test.
    • Please test first on a test folder.

Note

Solved by Richard.Williams

0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Rename file based on data in file », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).