我想编写一个 DOS 批处理过程,它将通过我的目录并将所有 *.txt 文件移动到 dest 文件夹以从 txt 文件的第一个字符开始
前任。
abc.txt 将移至文件夹“a”
def.txt 将移至文件夹“d”
,依此类推...
我想编写一个 DOS 批处理过程,它将通过我的目录并将所有 *.txt 文件移动到 dest 文件夹以从 txt 文件的第一个字符开始
前任。
abc.txt 将移至文件夹“a”
def.txt 将移至文件夹“d”
,依此类推...
从命令行:
for %i in (*.txt) do (set FOLDER=%i & move %i %FOLDER:~0,1%)
在批处理文件中,您必须将 % 加倍,如下所示:
for %%i in (*.txt) do (set FOLDER=%%i & move %%i %FOLDER:~0,1%)
我认为问题的关键是提取第一个字母。
您可以使用 ~ char 提取文件名的第一个字母
set str=filename
echo %str:~0,1%
f
您可以使用 biterscripting chex 命令(字符提取器)来提取文件名的第一个字符。这是脚本。假设您的文件位于 C:\My Directory。
# Script move1.txt
# Change directory to My Directory.
cd "C:\My Directory"
# Collect a list of *.txt files.
var str list ; lf -n "*.txt" > $list
# Go thru files one by one.
while ($list <> "")
do
# Get the next file.
var str file ; lex "1" $list > $file
# $file now has full path of a found *.txt file. Get just the file name.
var str filename ; stex -p "^/^l[" $file > $filename
# $filename has just the file name. Get the first character.
var str firstchar ; chex -p "1" $filename > $firstchar
# $firstchar now has the first char of file name. The folder where we
# want to move this file is, then, C:\Destimation Folder\$firstchar .
var str destfolder ; set $destfolder = "C:\Destination Folder\"+$firstchar
# Move $file to $destfolder.
system move ("\""+$file+"\"") ("\""+$destfolder+"\"")
done
脚本在 biterscripting ( http://www.biterscripting.com ) 中。要尝试,将脚本另存为 C:\Scripts\move1.txt,开始 biterscripting,输入以下命令。
script move1.txt
如果您需要定期运行脚本,请在任务调度程序中调度以下命令。
"C:\biterScripting\biterScripting.exe" "move1.txt"
我没有测试脚本,在示例文件上测试它。确保将“C:\My Directory”和“C:\Destination Folder”更改为正确的值。始终在文件名和路径中使用双引号。
怎么样这是一行:
一般形式:for %a in (abcdefghijklmnopqrstu vwxyz) do if not exist %a\nul md %a & if exists %a* move %a* %a
这个 mod 将只处理 txt 文件(根据 OP): ... 如果存在 %a*.txt move %a*.txt %a
或者可以在 bat 中增强此功能以执行“...如果存在 %%a*%1 移动 %%a*%1 %%a”以允许在命令行上进行扩展匹配;这可能需要 %1 中的点(现在不想测试它。如果这是一个问题,你总是可以有一个 if%1!==! 在一般形式和 %1 特定的形式之间进行选择已经提供了点。
当然,%a 用于命令行,对于 bat,将 %s 加倍正常(%a -> %%a)。
此解决方案将避免文件名中的空格错误,但您最终会得到每个字母的子目录(即使没有以该特定字母开头的文件)。
现在我输入这个比输入上面的原始行花费了更长的时间...... :-)
计算当前文件夹中的所有 TSV 文件。
for /f "delims=|" %%f in ('dir *.tsv /b') do (
echo %%~nf
findstr /R /N "^" %%f | find /C ":"
)
copy sourcepath\a*.txt destinationpath\a
复制源路径\b*.txt 目标路径\b