Solved help please! batch script not working

April 29, 2020 at 09:15:56
Specs: Windows 10
Hi everyone,

I am trying to create a batch file that will check if a specific file exists in a folder. However, the folder name changes everyday and I am trying to create a script that will select the "latest" folder everyday. Here is what I have so far:

@echo off

set "dest=\\Networkdrive\FolderA\FolderB\Ad_Hoc"
pushd "%dest%"

REM below code is to find the newest/latest folder
for /f "tokens=*" %%G in ('dir *.* /b /ad /od') do SET newest=%%G

REM now i am asking the file path to find the "newest folder" in which the files are located
set "source=%dest%\FLM\!newest!\FLM Batch\FolderC"
IF EXIST "%source%\Settlement Cashflows.xlsx" (
  ECHO "Settlement Cashflows.xlsx exists"
) ELSE (
  ECHO "Settlement Cashflows.xlsx does not exist"
)

popd
cmd /k

message edited by hiuser123


See More: help please! batch script not working

Reply ↓  Report •

#1
April 29, 2020 at 12:44:48
✔ Best Answer
In order to variables written like so to function !newest!, try changing:

@echo off

with

to @echo off&setlocal enabledelayedexpansion

specs: https://www.userbenchmark.com/UserR...

message edited by hidde663


Reply ↓  Report •

#2
May 2, 2020 at 16:36:13
You could try powershell:

$LatestFolder = Get-ChildItem -Path "insert path" | ? { $_.PSIsContainer } | Sort CreationTime -Desc | Select -f 1
$CheckFile = Test-Path -Path "$LatestFolder\file.xlsx"

if ($CheckFile -eq $True) {
Write-Host "File exist" -ForegroundColor "Green"
} else {
Write-Host "File do not exist" -ForegroundColor "Red"
}


Reply ↓  Report •
Related Solutions


Ask Question