【问题标题】:How to create folders and files from text file with same names to insert with corresponding name?如何从具有相同名称的文本文件创建文件夹和文件以插入相应名称?
【发布时间】:2025-04-05 12:50:02
【问题描述】:

我从门户网站同事的作品中受到启发

Have a batch create text files based on a list
Batch script to read input text file and make text file for each line of input text file

例如从文本文件开发创建
"comps.txt" ( comp1,comp2....
对于每台 PC,批处理脚本从文本文件中的 PC 列表中读取,并为每行文本创建一个文本文件作为本地文本。
稍后在代码中创建文件夹文件命名 - 相同的名称(comp1,comp2 .... 最后,我们有文本文件:comp1.txt、comp2.txt ... 和文件夹:comp1、comp2.... 直到 400 台计算机。
任何想法如何添加代码或编写另一个单独的批处理代码来移动每个对应的文件夹文本文件的文本文件

comp1.txt->comp1 folder  
comp2.txt->comp2.txt  
....  

我们有超过 400 行! 对于每天努力工作的任何脚本,我都是非常初学者,这是我的第一个问题。我在 Windows Batch 中的代码如下

@echo off
  setlocal
   for /f "tokens=*" %%a in (comps.txt) do (type nul>"%%a.txt")
  for /f "tokens=*" %%a in (comps.txt) do (
   echo This is line 1 of text>"%%a.txt"
   )
   for /f %%i in (comps.txt) do mkdir %%i 
  do (
  echo "%%i.txt"
      )
  endlocal

[]

【问题讨论】:

标签: windows file batch-file cmd directory


【解决方案1】:

生成文本文件然后生成文件夹以将文件移动到其中没有任何意义。首先创建文件夹并将文件创建到文件夹中。使用(代码块)只需要一个 for 循环。

@echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (
  if not exist "%%a" mkdir "%%a"
  echo This is line 1 of text>"%%a\%%a.txt"
)
endlocal

【讨论】:

  • 非常感谢 - 在您的帮助和代码的帮助下完美工作。这不仅与 txt 文件有关-对我来说,尤其是当您从“ .txt->.bat [echo This is line 1 of text>"%%a\%%a.txt" -> 更改内部代码扩展名时,这很有意义echo This is line 1 of text>"%%a\%%a.bat" ] 并且您的计算机文件夹名称具有相同的批处理脚本名称,以便进一步修改可以给定任何变量 - 我会看到 Tommorow -time 运行速度太快-问候
最近更新 更多