Processing multiple variables from input

November 3, 2011 at 07:38:36
Specs: Windows 7
I'm not sure the title is my exact problem, but i'm trying to run a batch file that moves files to the correct folder if it matches the folder name.

The current batch works as is but i'm manually adding any new foldernames into the batch as required. I'd like this automated and can't figure out how to create a Folderlist variable that can be used in place of the "nameX".

I've tried using the same FOR command as used at the start but only the last foldername in the list is set as the variable. I can't figure out how to get it to process each line through the batch.

The LoCase adjusts the case of the variable so it can match the string in case there is mixed case in the filename.

Thanks.

=========================
for /f "tokens=* delims=" %%a in ('dir/b /od *.txt'') do (
set FolderName=%%a)

SETLOCAL ENABLEDELAYEDEXPANSION
SET String=%FolderName%
SET String
CALL :LoCase String
SET String
SET FolderName=%String%

set FolderName="%FolderName:~0,5%"
IF %FolderName% == "name1" (set TAG=Name 1)
IF %FolderName% == "name2" (set TAG=Name 2)
IF %FolderName% == "name3" (set TAG=Name 3)

for /f "tokens=* delims=" %%a in ('dir/b /od *.txt') do (
set FileName=%%a)

move "J:\Folder\%FileName%" "J:\Folder\%TAG%"
=========================


See More: Processing multiple variables from input

Report •

#1
November 4, 2011 at 02:28:53
Both For loops are doing the same thing, looping through the Dir list until the end then exiting the loop so both FolderName and FileName will be set to the final entry in the respective list. If you want to process each entry in the list the commands must be within the For loop or be called from within the loop.

In the final loop the Move command line should be within the loop i.e. before the final )

At the moment the Move command line is outside the loop so is actioned only once.


Please come back & tell us if your problem is resolved.


Report •

#2
November 5, 2011 at 21:21:34
Thanks for the pointer in the right direction. Took a bit of troubleshooting but I got there in the end. It may not be the prettiest batch file but it does what I need it to.

=========================
@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=* delims=" %%a in ('dir/b /od *.txt') do (
set FileName=%%a & CALL :SetFolder
)

:SetFolder
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=* delims=" %%a in (list1.txt) do (
set FolderName=%%a & CALL :Process
)

:Process
SETLOCAL ENABLEDELAYEDEXPANSION
SET String=%FolderName%
CALL :LoCase String
SET FolderNameSHORT=%String%

SETLOCAL ENABLEDELAYEDEXPANSION
SET String=%FileName%
CALL :LoCase String
SET FileNameSHORT=%String%

IF "%FolderNameSHORT:~0,5%" == "%FileNameSHORT:~0,5%" (move "J:\Folder\%FileName%" "J:\Folder\%FolderName%")


:LoCase
:: Subroutine to convert a variable VALUE to all lower case.
:: The argument for this subroutine is the variable NAME.
SET %~1=!%1:A=a!
SET %~1=!%1:B=b!
SET %~1=!%1:C=c!
SET %~1=!%1:D=d!
SET %~1=!%1:E=e!
SET %~1=!%1:F=f!
SET %~1=!%1:G=g!
SET %~1=!%1:H=h!
SET %~1=!%1:I=i!
SET %~1=!%1:J=j!
SET %~1=!%1:K=k!
SET %~1=!%1:L=l!
SET %~1=!%1:M=m!
SET %~1=!%1:N=n!
SET %~1=!%1:O=o!
SET %~1=!%1:P=p!
SET %~1=!%1:Q=q!
SET %~1=!%1:R=r!
SET %~1=!%1:S=s!
SET %~1=!%1:T=t!
SET %~1=!%1:U=u!
SET %~1=!%1:V=v!
SET %~1=!%1:W=w!
SET %~1=!%1:X=x!
SET %~1=!%1:Y=y!
SET %~1=!%1:Z=z!
SET %~1=!%1:.= !

:end


Report •

#3
November 5, 2011 at 23:14:58
Thanks for coming back to report your success.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
November 9, 2011 at 00:16:53

I've run into a small hitch. If there is no folder that matches the filename, then the batch ends on that file even if there might be other files that will work.

I thought of just moving all the files to the root folder at the start or the end of the batch, but as the batch doesn't complete if there isn't a folder match, that solution doesn't work.

I can't think of how to make it so once all the folder variables are run through it then just moves the file to the root folder.

Adding an IF NOT or ELSE command also doesn't work as the first instance that the strings don't match it makes the IF NOT or ELSE command active, using the current variables which are not correct.


Report •

Ask Question