2015-04-05 84 views
0

我有數百個csv文件。 csv文件存儲在文件夾和子文件夾中。我想搜索其文件名已確定的五十個csv文件,例如1.csv,2.csv,3.csv,...,50.csv。如果我使用Windows搜索工具逐一搜索,那麼非常麻煩。我想如果找到這些文件,請保存在名爲FOUND的文件夾中。請使用批量編程/蝙蝠幫助解決此問題?非常感謝你批量編程搜索子文件夾中的某些文件

回答

1

有很多方法可以採用,具體取決於你需要多少自動化...爲了幫助你開始,你可能想看看它幫助我的this(並且確實繼續這樣做)當我開始學習批次時。此外,我會提供一個可能的模板爲達到您的目標,因爲我已經解釋它。也許它不是最優雅或有效的方法,但它引入了一些批處理命令,你可能會也可能沒有遇到,這反過來可能會幫助你開發自己的方法。

@echo off 
setlocal enabledelayedexpansion 

echo Please enter a drive letter: 
set /p "drive=>" 
echo Please enter a search string: 
set /p "searchstring=>" 
echo %searchstring%>search.txt 

set /p search=<search.txt 
set /a suffix=0 
echo.>>search.txt 

:LOOP 
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1 
set seq=%search% !suffix! 
echo !seq!>>search.txt 
) 
if !suffix! leq 49 goto LOOP 

for /f "tokens=*" %%i in (search.txt) do (
    for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
    if not exist "%~dp0\found" md "%~dp0\found" 
    move /y "%%j" "%~dp0\found\%%~nxj" 
    ) 
) 
pause 

這是不打算作爲一個明確的解決方案,但你會發現它回答您的原始查詢/請求。祝一切順利。

1

下面是你一個可行的解決方案..

@ECHO OFF 
SETLOCAL EnableDelayedExpansion 

REM First Set your directories input and output 
SET InputDir=C:\Directory to your CSV files\ 
SET OutputDir=C:\Directory to your CSV files\FOUND 

REM check if the FOUND directory exist, if not, then create it. 
IF NOT EXIST OutputDir (
mkdir %OutputDir% 
) 

REM Grab a scan of the input directory and save it to a temporary file list. 
Dir /a /b %InputDir%>"%OutputDir%\Found.txt" 

REM Set the files you would like to find. 
SET "File1=1.csv" 
SET "File2=2.csv" 
SET "File3=50.csv" 

REM The loop, to process the matching file(s). 
FOR %%A IN (%File1%,%File2%,%File3%) DO (
    FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
    IF %%A==%%B (
    copy "%InputDir%\%%A" "%OutputDir%\%%A" 
    ) 
) 
) 

REM Clean up the temp file list. 
DEL "%OutputDir%\Found.txt" 

請記,我沒有報價增加了輸入和輸出變量,而是加引號的代碼複製部分以補償白色您的目錄路徑中有空格。我試圖保持簡單,所以你可以按照它所處理的邏輯來處理你正在尋找的東西,現在你可以根據你的喜好修改它。玩得開心。乾杯!

相關問題