2016-08-01 81 views
0

每個星期五晚上文件將被放置在一個目錄中。我正在編寫一個批處理文件來歸檔文件(使用簡單的複製命令),然後我需要重命名並將文件加載到OLAP數據庫中。不幸的是,我不知道每週的文件名是什麼,因爲它是基於日期和時間戳的 - 我知道的唯一的事情就是將以.txt結束。根據文件夾中的文件創建變量

由於這將是該文件夾中唯一的文件,是否有可能創建一個基於文件名的變量?

這是我到目前爲止有:

if not exist "%LoadFolder%\*.txt" (
    rem Send email notification that the latest Exchange Rates file does not exist... 
    "E:\Programs\PuTTY\plink.exe" -v -ssh servername -pw password /u02/hyp_app/Scripts/STFC/STFC_File_Not_Exist.ksh 
    exit 
) else (
    rem 1) Copy latest file to Archive folder... 
    FOR /F "delims=|" %%I IN ('DIR "%LoadFolder%\*.txt" /B /O:D') DO SET NewestFile=%%I 
    copy "%Load%\%NewestFile%" "%LoadFolder%\Archive\%NewestFile%" 

    rem 2) Rename the latest file to Load_File.txt... 
    FOR /F "delims=|" %%I IN ('DIR "%LoadFolder%\*.txt" /B /O:D') DO SET NewestFile=%%I 
    REN "%LoadFolder%\%NewestFile%" "Load_File.txt" 
) 

REM rest of code.......... 

我的問題是,該文件夾重命名爲LOAD_FILE而不是在文件夾中的文件。我究竟做錯了什麼?

+0

請編輯您的問題,使用CODE按鈕在文本窗口中正確格式化您的代碼。 – sambul35

+0

[調試你的批處理文件](http://www.robvanderwoude.com/battech_debugging.php) – DavidPostill

回答

0

假設存檔文件夾存在,請嘗試此腳本併發布確切的錯誤(如果有的話)。在SS64 CMD給

@echo off 
setlocal enabledelayedexpansion 
set LoadFolder=yourFolderPath 
if not exist "%LoadFolder%\*.txt" (
rem Send email notification that the latest Exchange Rates file does not exist... 
"E:\Programs\PuTTY\plink.exe" -v -ssh servername -pw password /u02/hyp_app/Scripts/STFC/STFC_File_Not_Exist.ksh 
) else (rem Copy latest file to Archive folder and rename it at source dir 
    FOR %%I IN ("%LoadFolder%\*.txt") DO (
     copy %%I "%LoadFolder%\Archive\%%~nxI" 
     ren %%I Load_File%%~xI)) 
timeout 5 
exit /b 

瞭解更多關於path modifiers:你真正的文件夾路徑替換yourFolderPath

+0

sambul35,謝謝你的幫助。您的代碼似乎正在工作。 – Jack56

相關問題