IF Exist How to setup sub folders in a batch file

女生的网名这么多〃 提交于 2021-01-20 12:17:33

问题


I can't seem to make this code work

Original Works if I make it solo and add files in same folder as the bat

@echo off
for %%a in (*.txt) do ren "%%a" "Tier 1) - %%a.tmp"
ren *.tmp *.

Trying to add it to my IF Exist bat file

IF %ERRORLEVEL% EQU 6 ( @echo off
IF EXIST "01_FileRename\Tier_1\Activate" (
for %%a in (*.txt) do ren "%%a" "Tier 1) - %%a.tmp"
ren *.tmp *.

I have 8 functions in my IF Exist bat file trying to add this function in
what the code does
2004_Honda_ Accord.txt
Tier 1 2004_Honda_Accord.txt

How do I setup my Directory
I have .txt files inside the Tier_1 folder and I need them all to be edited

How can I make this work

IF %ERRORLEVEL% EQU 6 ( @echo off
IF EXIST "01_FileRename\Tier_1\Activate" (
echo on
REM This lines tells the user what this bat file is doing
call ant
call 01_FileRename\Tier_1\Add Tier Text.bat

Can this code work and how can I make it work
I've been at this for 3 days now
I was trying to run my bat file to add name from another bat file in
a different folder


回答1:


I excavated most of the needed information from the comments on your other question and did some guess-work for the rest.

I answer here because the other question is closed and this one is surprisingly not.

The renaming code itself is just one logical line, consisting of two nested for loops and the ren command.

@echo off 
REM the following indented lines are just to produce a test environment.
    md "Tier 1" "Tier 2" "Tier 3"
    break>"Tier 1\2004_Honda_Accord.txt"
    break>"Tier 1\2005_Ford_Focus.txt"
    break>"Tier 2\2006_Opel_Corsa.txt"
    break>"Tier 3\2007_VW_Golf.txt"
    break>"do not touch me.txt"
REM now the code part begins.     
echo ----- before:
tree /f

for /d %%d in (*) do (
  for %%f in ("%%d\*") do (
    ren "%%f" "%%d %%~nxf"
  )
)
echo ----- after:
tree /f

Output:

----- before:
Auflistung der Ordnerpfade für Volume Data
Volumeseriennummer : 34F8-3334
D:.
│   do not touch me.txt
│   test.bat
│
├───Tier 1
│       2004_Honda_Accord.txt
│       2005_Ford_Focus.txt
│
├───Tier 2
│       2006_Opel_Corsa.txt
│
└───Tier 3
        2007_VW_Golf.txt

----- after:
Auflistung der Ordnerpfade für Volume Data
Volumeseriennummer : 34F8-3334
D:.
│   do not touch me.txt
│   test.bat
│
├───Tier 1
│       Tier 1 2004_Honda_Accord.txt
│       Tier 1 2005_Ford_Focus.txt
│
├───Tier 2
│       Tier 2 2006_Opel_Corsa.txt
│
└───Tier 3
        Tier 3 2007_VW_Golf.txt

See for /? for more information on the for command.

As you seem to be quite new to batch, I recommend bookmarking SS64 as a command reference.



来源:https://stackoverflow.com/questions/64398671/if-exist-how-to-setup-sub-folders-in-a-batch-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!