bit new at this and hoping someone can help. I have the following batch script that queries a database, pulls back a result and sets it to a variable.
set "sqlcmdServer=sqlinstance"
for /f %%A in (
'sqlcmd -h -1 -r 1 -Q "SET NOCOUNT ON; Declare @v varchar(100) set @v = (select idcorrent from database.dbo.table (nolock) where [identity] = 'UK' and idletter = 'EX' and stcorres = 14) Select @v"'
) do set "var=%%A"
echo %var%
pause
This very nicely brings me back a string. What i would like to then do is use this variable within the same script, to search a folder on a network drive for a single file (pdf in amount a shed load of other files specifically) that contains this variable. The filename will always end with the variable i.e. "somecharacters_variable.pdf"
I was messing around with this:
for /f "delims=" %%s in ('xcopy /l /e /y "C:\Users\USERNAME\Desktop\test1\*.pdf"r "C:\Users\USERNAME\Desktop\test2" ^|find ":"') do copy "%%s" "C:\Users\USERNAME\Desktop\test2"
which, when testing, successfully found my test file and moved it from one desktop folder to another. So I tried adding it to the end of my batch script, using setlocal enabledelayedexpansion and replacing desktop dir with the network drive and inserting the variable as such(changed network drive name for purpose of this question):
set "sqlcmdServer=sqlinstance"
setlocal enabledelayedexpansion
for /f %%A in (
'sqlcmd -h -1 -r 1 -Q "SET NOCOUNT ON; Declare @v varchar(100) set @v = (select idcorrent from database.dbo.table (nolock) where [identity] = 'UK' and idletter = 'EX' and stcorres = 14) Select @v"'
) do set "var=%%A"
echo !var!
for /f "delims=" %%s in ('xcopy /l /e /y "\\networkdrive\test\test\*!var!.pdf"r "C:\Users\USERNAME\Desktop\test2" ^|find ":"') do copy "%%s" "C:\Users\USERNAME\Desktop\test2"
But this didn't work.
is it achievable the way I'm trying? if not how would i go about it?
Any help/guidance is appreciated
Kind regards
rappended to thexcopysource, which I guess is unintended... - aschipflC:\Users\USERNAMEyou could use%USERPROFILE%to point to the directory of the currently logged on user... - aschipfl