Windows 批处理文件名生成器没有运行,我哪里做错了?

Windows 批处理文件名生成器没有运行,我哪里做错了?

我正在编写一个 Windows 批处理脚本,以 .csv 作为输入,将每行中的数据作为变量读取,并将唯一文件名输出到 Windows 批处理脚本中的新 .csv。我这样做是为了创建一个包含数千张模拟幻灯片的清单,这些幻灯片我正在转换为数字。

由于某种我无法弄清楚的原因,它没有创建输出,如果有人能够对出现的问题提供一些见解,我将不胜感激。

@echo off setlocal enabledelayedexpansion

set prefix="MyPrefix" set input_file="input.csv" 
set output_file="filenames.csv" 
set log_file=log.txt

rem Set up logging echo Starting the script > !log_file!

!output_file! ( for /f “skip=1 tokens=1-3 delims=,” %%a in (!input_file!) do ( set “box_num=00%%a” set “folder_num=00%%b” set “image_range=%%c” for /l %%i in (1, 1, !image_range!) do ( set “num=000%%i” set “filename=!prefix!_Box!box_num:~-2!_Folder!folder_num:~-2!_image!num:~-4!” echo !filename! ) echo Generated filenames for box !box_num:~-2!, folder !folder_num:~-2!, and image range !image_range! >> !log_file! ) )

echo Finished the script >> !log_file! echo The script has successfully completed.
A C
1 1 75
17 1 71
17 2 78

抱歉,该网站还不允许我发布直接从 Excel 粘贴的内容,但这与之相同。

答案1

@echo off 
setlocal enabledelayedexpansion

cd /d "%~dp0"
set "_prefix=.\MyPrefix"
set "_output=.\filenames.csv" 
set "_log=.\log.txt

for /f "skip=1 tokens=1-3 delims=," %%a in (' type .\"input.csv" ') do (
     set "_box=00%%~a"
     set "_folder=00%%~b"
     set "_range=000%%~c"
     for /l %%i in (10001, 00001,10!_range:~-3!) do (
         set "_num=%%~i"
         set "_filename=!_prefix!_Box!_box:~-2!_Folder!_folder:~-2!_image!_num:~-4!"
         echo\!_filename!
        ) 
         echo Generated filenames for box !_box:~-2!, folder !_folder:~-2!, and image range !_range:~-4!
    )

endlocal

“由于某种我无法弄清楚的原因,它没有创建输出,如果有人能够对出了什么问题提供一些见解,我将不胜感激。”

您的重定向>here.csv ( commands ... >>here_too.log)命令解释器将选择哪一个?

因此,重新考虑一下你想在屏幕上看到什么,以及你想在.csv文件和你想要的.log文件,但要明白解释器不提供到 std_out 的多重重定向(stderr 也不提供)。

优先考虑正确的代码执行,然后单独>>附加.csv,和/或>>附加.log


据我所知,什么进入屏幕,什么进入文件,您可以使用“技巧”并使用以下命令在屏幕上显示响应行:

>some.txt ( 
   command 
   command
   >con: echo/ out this on screen
  )
  • 提出此建议的原因如下:
@echo off && setlocal enabledelayedexpansion

cd /d "%~dp0"

set "_log=.\log.txt 
set "_prefix=.\MyPrefix"
set "_output=.\filenames.csv" 

2>nul del /q /f /a: .\"filenames.csv"

for /f skip^=1^tokens^=1-3^delims^=^, %%a in =;(' type .\"input.csv"
    ');= do >>.\filenames.csv =;(
     set "_box=00%%~a"
     set "_folder=00%%~b"
     set "_range=000%%~c"
     for /l %%i in =;( 10001,00001,10!_range:~-3! );= do =;(
         set "_num=%%~i"
         set "_filename=!_prefix!_Box!_box:~-2!_Folder!_folder:~-2!_image!_num:~-4!"
         echo\!_filename!
        );= 
     @=>con: echo Generated filenames for box !_box:~-2!, folder !_folder:~-2!, and image range !_range:~-4!
    );=

endlocal

相关内容