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 21416 times)

0 Members and 1 Guest are viewing this topic.

strokeman

  • Guest
need advanced batch file assistance please
« on: January 27, 2013, 10:01:41 PM »
 ??? i want to write a batch file that creates a dir.tmp and processes that to make a directory of the same name LESS the .TBN extension, so it needs to rtrim te string and parse back to %G. Otherwise it will try and create a dir with the same name as the file and crash! you can see the commented out attempt to concatenate.
@cls
SET MyVar=.TBN
@ECHO OFF
COLOR 70
ECHO ------------------------------
ECHO XBMC TBN TO DIR Creator V0.99
ECHO ------------------------------
ECHO be sure you have run a seperate
echo export first, you will need to
echo run a database rebuild next....
pause
COLOR
@echo off
DIR /B *.TBN >DIR.TMP
FOR /F %%G IN (DIR.TMP) DO MD %%G
:Call :TRIM %%MyVar%%
:Echo MyVar=[%%MyVar%%]
:Set MyVar =
:echo %%G
:GoTo :EOF
:TRIM
:Set MyVar=%%*
:GoTo :EOF
MD %%G
:move /Y %%G.* ..%%G

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: need advanced batch file assistance please
« Reply #1 on: January 27, 2013, 11:28:23 PM »
Wait! what is wrong with a directory and file having the same name?
It is not clear what you want to do.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #2 on: January 28, 2013, 03:23:05 AM »
I hope I understood what you were after:

Code: [Select]
@echo off
for %%a in (*.tbn) do md "%%~na"

If you want to move the set of files into the corresponding new folder then this should work:

Code: [Select]
@echo off
for %%a in (*.tbn) do md "%%~na" & move "%%a" "%%~na"

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #3 on: January 28, 2013, 03:27:05 AM »
Wait! what is wrong with a directory and file having the same name?

It's not possibubble, when they are in the same folder, if the folder has the extension too.

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #4 on: January 28, 2013, 10:31:30 PM »

If you want to move the set of files into the corresponding new folder then this should work:

Code: [Select]
@echo off
for %%a in (*.tbn) do md "%%~na" & move "%%a" "%%~na"

assuming %~na is the truncated result, i.e tiger from tiger.tbn, what do i use to copy tiger.* wildcards ? what is %a the tiger.tbn filename?

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: need advanced batch file assistance please
« Reply #5 on: January 28, 2013, 10:53:38 PM »
Possibubble ? ?
" Anyone who goes to a psychiatrist should have his head examined. "

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #6 on: January 29, 2013, 01:52:04 AM »
Possibubble ? ?

Yep.  It's impossibubble.  And the people at the hostipital told me so.

If the file is named file.txt then you can't also have a folder named file.txt in the same location.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #7 on: January 29, 2013, 01:54:52 AM »
assuming %~na is the truncated result, i.e tiger from tiger.tbn, what do i use to copy tiger.* wildcards ? what is %a the tiger.tbn filename?

if you have a file called tiger.tbn and also want to move tiger.txt and tiger.pdf then you can use this:
Code: [Select]
@echo off
for %%a in (*.tbn) do md "%%~na" & move "%%~na.*" "%%~na"

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #8 on: January 29, 2013, 09:51:32 PM »
that did the trick, but more to the point i understand it now

i have been trying to echo a blank line, normally id use ALT+ 032 I Think for a blank char, but this keyboard is made for stroke tetraplegics of which im one

lastly i wish to create a blank file as a marker in an EXIST loop, but without user intervention, ie a >nul, what can i do?, edlin reqs ^Z, for example... :(

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #9 on: January 29, 2013, 11:12:26 PM »
The two ways to test for a folder and a file.  Note the trailing backslash when testing for a folder.

if exist "c:\folder\" type nul >"c:\folder\flagfile.bin"

if exist "c:\autoexec.bat" type nul >"c:\flagfile.bin"

Salmon Trout

  • Guest
Re: need advanced batch file assistance please
« Reply #10 on: January 30, 2013, 03:36:18 AM »
A third way... use the ~a variable modifier with FOR like this

for %%I in ("name") do set AttributeString=%%~aI

"name" is a file or folder name (which must exist!) e.g. "d:\temp\test.txt" or "C:\" or "D:" (no trailing slash needed for folders)

AttributeString is an arbitrary name I chose to hold the result which is a string 9 characters long which has either a lower case letter or a dash in each character position like this: drahscotl. If the attribute is set, the letter is present, if the attribute is unset, there is a dash.

Not all the attributes may be present on all file systems, but they are all possible on NTFS.

for a folder with no attributes set you would have d--------
for a folder which is hidden, system and a junction point or link*, e.g. "My Documents" you see d--hs---l
for a file ready for archiving with no special attributes you would see --a------

the letters mean:

d a directory
r read only
a ready for archiving
h hidden
s system
c compressed
o offline
t temporary
l link/junction point*

Thus:

if "%AttributeString:~0,1%"=="d" then name is a directory
if "%AttributeString:~0,1%"=="-" then name is NOT a directory

* http://comptb.cects.com/2268-overview-to-understanding-hard-links-junction-points-and-symbolic-links-in-windows



« Last Edit: January 30, 2013, 04:06:59 AM by Salmon Trout »

Salmon Trout

  • Guest
Re: need advanced batch file assistance please
« Reply #11 on: January 30, 2013, 04:56:14 AM »
Note: the c (compressed) attribute refers to NTFS compression, not any other type such as Zip.

Salmon Trout

  • Guest
Re: need advanced batch file assistance please
« Reply #12 on: January 30, 2013, 05:46:11 AM »
a fourth way...

dir /ad /b "name">nul 2>&1 && (set isfolder=yes) || (set isfolder=no)



foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: need advanced batch file assistance please
« Reply #13 on: January 30, 2013, 06:51:16 PM »
Yet another way.  :D


dir "d:\folder"|find ">">nul && echo it's a directory

dir "d:\file.txt"|find ">">nul || echo it's a file

strokeman

  • Guest
Re: need advanced batch file assistance please
« Reply #14 on: February 01, 2013, 09:00:18 PM »
why is the condition never met?
@cls
@ECHO Off
ECHO +-----------------------------+
ECHO :XBMC NFO TO DIR Creator  V1.0:
ECHO +-----------------------------+
ECHO.

IF NOT EXIST NFO2DIR.TXT (
 ECHO Don't be a bigshot, R.T.F.M Before you run, Press ^^C if you want to abort. .
 ping localhost -n 4 >nul
   ) else (
   START notepad /P NFO2DIR.TXT
   TYPE NUL>"nfo2dir.prn"
   goto bypass
)

if exist nfo2dir.prn (
 echo Printed already. . .
 ping localhost -n 4 >nul
)

if exist nfo2dir.txt (
 START /MAX notepad nfo2dir.txt
)

:bypass
IF NOT EXIST *.NFO (
 ECHO It appears you haven't run vid. export yet!
 ping localhost -n 4 >nul
 exit
)

if not exist "nfo2dir.done" (
 type nul >"nfo2dir.done"
   ) else (                                                                                         
   ECHO You have processed already
   ping localhost -n 4 >nul
   exit
)

echo Please wait. . DON'T INTERUPT!!
ping localhost -n 4 >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. . .
)

dir /s ::do directory list including subdirs.
@cls
echo Operation Completed Successfully.