Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need advanced batch file assistance please  (Read 21449 times)

0 Members and 1 Guest are viewing this topic.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #30 on: February 27, 2013, 09:24:24 PM »
fair comment, ill try to be more specific,
lets assume there r 2 files, named test-cd1.avi and test-cd2.avi
"        "     " the batch file is test.bat
so we enter test -cd1 4 at commandline

The batch file is called test.bat
So you entered test -cd1 4

But you are not passing the filename - how does the batch file know which filename(s) to operate on?

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #31 on: February 28, 2013, 07:33:51 PM »
it changes each iteration, its from the for/do loop which processes the wildcard based on %1, in this case *-cd1.*. i guess im wanting -cd1 removed from the current file. the %2 giving the number of chars to remove, tho would much rather have a function that determines this from %1.

so its what to do with

for %%a in (*%1.*) do md "%%~na" & move "%%~na.*" "%%~na"

clear as mud?

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #32 on: February 28, 2013, 10:40:29 PM »
If you are trying to sort groups of files but there are numerous suffixes on them, then you can do it in batches semi-manually.

This should take avi files ending in -cd1, -cd2, -cd3, -cd4 (or -cd with any next character) and move them into respective folders, named from the filename without the ending 4 characters.


Code: [Select]
@echo off
set end=-4
for %%a in (*-cd?.avi) do call :next "%%a"
echo done
pause
goto :EOF
:next
set "var=%~n1"
call set "var=%%var:~0,%end%%%"
md "%var%" 2>nul
echo moving %1 to "%var%"
move "%~1" "%var%" >nul


repeat with different filespecs and the appropriate number for the length of the suffix.

Filenames with % and ^ in them could cause issues.

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #33 on: March 01, 2013, 07:19:21 PM »
u make it look easy!, im struggling to understand it
works fine putting in each variation on commandline
now if only i could get it todo 3 increments, without changing %1 and running it again.
how do we add 1 to %%a?, is it even poss.?, what about doing stuff to %2?

@echo off
set end=-%2
for %%a in (*%1.*) do call :next "%%a"
goto :EOF
:next
set "var=%~n1"<---w t f is n1?
call set "var=%%var:~0,%end%%%"
md "%var%" 2>nul <--- w t f does the 2 do?
move "%~1" "%var%" >nul

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #34 on: March 01, 2013, 10:19:19 PM »
now if only i could get it todo 3 increments, without changing %1 and running it again.

We are guessing what you are doing.  It's hard to give advice without knowing the task.


set "var=%~n1"<---w t f is n1?

Read the help for for /? and you'll find it at the end.

md "%var%" 2>nul <--- w t f does the 2 do?

In this case it eliminates a message in STDERR about the folder already existing.   The error message goes to NUL when the folder already exists.


Salmon Trout

  • Guest
Re: need advanced batch file assistance please
« Reply #35 on: March 02, 2013, 02:46:30 AM »

md "%var%" 2>nul <--- w t f does the 2 do?

In this case it eliminates a message in STDERR about the folder already existing.   The error message goes to NUL when the folder already exists.

My way of getting around that is to do an IF EXIST test e.g. if not exist logfolder md logfolder

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #36 on: March 02, 2013, 06:44:27 AM »
My way of getting around that is to do an IF EXIST test e.g. if not exist logfolder md logfolder

That's the traditional and effective and sensible way - I'm lazy in some respects and go for the 2>nul these days.

I do the same with del filename.txt 2>nul

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #37 on: March 02, 2013, 02:44:45 PM »
can live with your solution, but surely there's a function about to count the chars in %1 instead of %2?

eg commandlines

-cd1 4
.disk1 6

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: need advanced batch file assistance please
« Reply #38 on: March 02, 2013, 06:48:48 PM »
Code: [Select]
@echo off
set a=%1
setlocal EnableDelayedExpansion
set b=1

:loop.countChar.2
call set c=%%a:~!b!,1%%
if "%c%"=="" goto end
set /a b+=1
goto loop.countChar.2

:end
echo %2 %b%
pause

will count the number of characters in argument 1 (%1). It's kinda long, but works relatively well.

Code: [Select]

C:\Users\Lemonilla\Documents\bat>call countCharInArg2.bat thisIs16CharLong 1
thisIs16CharLong                16
Press any key to continue . . .

C:\Users\Lemonilla\Documents\bat>


EDIT: misread your post, fixed code to count from arg 1 and not arg 2.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #39 on: March 04, 2013, 07:50:32 PM »
I know this doesn't work, how can i pass no .nfo file, AND no commandline given?

IF NOT EXIST *.NFO & z=%1z (
  ECHO It appears you haven't run vid. export yet!
  ping localhost -n 5 >nul
  exit /B
)

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: need advanced batch file assistance please
« Reply #40 on: March 04, 2013, 08:10:51 PM »
Code: [Select]
if not exist *.nfo (
    if "%1z"=="z" (
        ECHO It appears you haven't run vid. export yet!
        ping localhost -n 5 >nul
        exit /B
    )
)
not sure what you mean by =z, but this should only activate if both statements return true.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #41 on: March 04, 2013, 08:52:36 PM »
its my solution and likely the wrong 1. This is TRUE only when no %1 exists, ie 'z'=='z'. what is right way IF exist %1==""?
aaahhh, wouldnt have thought of nesting loops. thanx

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #42 on: March 04, 2013, 09:04:15 PM »
is fine but not processing %1, whats wrong?

@cls
@ECHO Off
ECHO +-----------------------------+
ECHO :XBMC NFO TO DIR Creator  V2.0:
ECHO +-----------------------------+
ECHO.

IF not EXIST NFO2DIR.TXT (
     ECHO Ensure yourv'e read nfo2dir.txt before you run, Press ^^C if you want to abort. .
   echo        Because WARNING: This will irreversibly, alter disk structure!!!
   ping localhost -n 5 >nul
 ) else (
 START /max notepad NFO2DIR.TXT
)
   
if not exist *.nfo (
    if "%1z"=="z" (
        ECHO It appears you haven't run vid. export yet!
        ping localhost -n 5 >nul
        exit /B
    )
)
echo Please wait. . DON'T INTERRUPT!!
ping localhost -n 5 >nul

for %%a in (*.NFO) do md "%%~na" & move /y "%%~na.*" "%%~na" & move /y "%%~na-fanart.*" "%%~na" & move /y "%

%~na-poster.*" "%%~na" & move /y "%%~na-thumb.*" "%%~na"

if EXIST extrathumbs (
 rd /s /q extrathumbs
)

if EXIST extrafanart (
 rd /s /q extrafanart   
 echo extrafanart and extrathumbs dir. deleted
   ) ELSE (
   echo extrafanart or extrathumbs dir. not found. . .
)

if not x==%1x goto end
set a=%1
setlocal EnableDelayedExpansion
set b=1
:loop.countChar.2
call set c=%%a:~!b!,1%%
if "%c%"=="" goto end
set /a b+=1
goto loop.countChar.2
:end
echo Stacked files may exist, no switches given, read manual
ping localhost -n 5 >nul
dir /p
exit /b

set end=-%b%
for %%a in (*%1.*) do call :next "%%a"
goto :EOF
:next
set "var=%~n1"
call set "var=%%var:~0,%end%%%"
md "%var%" 2>nul
move "%~1" "%var%" >nul

@cls
echo Operation Completed Successfully.
ping localhost -n 5 >nul

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: need advanced batch file assistance please
« Reply #43 on: March 05, 2013, 05:02:19 AM »
is there a reason that it uses %1z and not %1 in the if statement? The only thing I can think of for z would be %:~z1 which is file size, and I don't think that's what you were looking for.  the if statement would only work if "%1%"=="", or is undefined.

oh, and if %1 is more than one word, then you need to put quotes around it in all your statements, otherwise some will not run properly.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #44 on: March 05, 2013, 07:29:39 PM »
is there a reason that it uses %1z and not %1 in the if statement? The only thing I can think of for z would be %:~z1 which is file size, and I don't think that's what you were looking for.  the if statement would only work if "%1%"=="", or is undefined.

oh, and if %1 is more than one word, then you need to put quotes around it in all your statements, otherwise some will not run properly.

when theres not a %1 given, ie its blank z or x,y,z whatever you give it, z==z, so a TRUE condition is met.

just wanted foxidrive and lemonilla to double check their respective code. post insertion they worked fine, so must be a syntaxy/typo thing going on.