FTP Powershell - batch file not working in windows 7.

The same batch file was working in windows xp. But does not work in windows 7. Scheduler works but process of the batch file stops and does not run. Need help in fixing this.

I have uploaded the batch file.
Batch file uploaded as text file.
upload.txt
LVL 6
anumosesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anumosesAuthor Commented:
I feel few commands are not being recognized by windows 7.
anumosesAuthor Commented:
Is there a way to convert file variable to string variable and I think an extra space is created and the process quits.
Steven CarnahanNetwork ManagerCommented:
Can you provide the errors you get when you run it?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

anumosesAuthor Commented:
@echo off
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=C:\Purple_Top
set logfile=upload.log
set ArchiveFolder=%CsvFolder%\Archive
set CsvFile=
if not exist "%ArchiveFolder%" md "%ArchiveFolder%"
rem for /f "delims=" %%a in ('dir /o:d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
for /f "delims=" %%a in ('dir *.csv /b') do set CSVFile=%%a
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)


echo Using '%CsvFile%' as input file.
for %%a in ("%CsvFile%") do (set UploadFile=%%~dpna.csv)

pause

echo Filtering unused columns, writing output to '%UploadFile%'.
powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '%CsvFile%' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -FilePath '%UploadFile%' -Encoding ASCII}"

call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

REM ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"
REM del "%FtpFile%"
move "%CsvFile%" "%ArchiveFolder%"
REM del "%UploadFile%"

REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
REM open heart1
REM hbc_data
REM hbc_data
REM cd /home/hbc_data/data/purple_top_data
REM mput %UploadFile%
REM bye

Open in new window


I did a code fix and wrote another script to run this. So runs locally on C drive.

This code

Start "Upload" "F:\Purple_Top\upload2.bat"

executes

No I need help

1. my script has to be modified
to check for csv file exists in F drive

2. Copy to C drive and mv to archive on f drive

3. Execute the below

Start "Upload" "F:\Purple_Top\upload2.bat"
anumosesAuthor Commented:
C:\>@echo off
'@echo' is not recognized as an internal or external command,
operable program or batch file.

C:\>setlocal enabledelayedexpansion

C:\>set FtpFile=F:\Purple_Top\upload.ftp

C:\>set CsvFolder=C:\Purple_Top

C:\>set logfile=upload.log

C:\>set ArchiveFolder=C:\Purple_Top\Archive

C:\>set CsvFile=

C:\>if not exist "C:\Purple_Top\Archive" md "C:\Purple_Top\Archive"

C:\>rem for /f "delims=" %a in ('dir /o:d /b "C:\Purple_Top\*.csv" ^| findstr.ex
e "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do
 (set CsvFile=C:\Purple_Top\%a)

C:\>for /F "delims=" %a in ('dir *.csv /b') do set CSVFile=%a

C:\>set CSVFile=10_23_2015_11_45.csv

C:\>if not defined CsvFile (
echo No csv file found in 'C:\Purple_Top'.
 exit /b 1
)

C:\>echo Using '10_23_2015_11_45.csv' as input file.
Using '10_23_2015_11_45.csv' as input file.

C:\>for %a in ("10_23_2015_11_45.csv") do (set UploadFile=%~dpna.csv )

C:\>(set UploadFile=C:\10_23_2015_11_45.csv )

C:\>pause
Press any key to continue . . .

C:\>echo Filtering unused columns, writing output to 'C:\10_23_2015_11_45.csv'.

Filtering unused columns, writing output to 'C:\10_23_2015_11_45.csv'.

C:\>powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '
10_23_2015_11_45.csv' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT
(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-
Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -
FilePath 'C:\10_23_2015_11_45.csv' -Encoding ASCII}"
Out-File : Access to the path 'C:\10_23_2015_11_45.csv' is denied.
At line:1 char:260
+ & {Import-Csv -Path '10_23_2015_11_45.csv' | Select-Object -Property 'Sample
ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-
Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Tr
im([char]34)} | Out-File <<<<  -FilePath 'C:\10_23_2015_11_45.csv' -Encoding AS
CII}
    + CategoryInfo          : OpenError: (:) [Out-File], UnauthorizedAccessExc
   eption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand


C:\>call :ExportDataSection FTP "F:\Purple_Top\upload.ftp"

C:\>REM *** Reads all lines listed in FTP (section) and writes them to "F:\Purpl
e_Top\upload.ftp" (file name).

C:\>REM *** Environment variables will be expanded.

C:\>set Section=FTP

C:\>set FileName=F:\Purple_Top\upload.ftp

C:\>if exist "F:\Purple_Top\upload.ftp" del "F:\Purple_Top\upload.ftp"

C:\>for /F "tokens=1 delims=[]" %a in ('type "F:\Purple_Top\upload2.bat" | C:\Wi
ndows\system32\find.exe /n "[FTP]"') do set DataStart=%a

C:\>set DataStart=59

C:\>for /F "skip=59 delims=" %a in ('type "F:\Purple_Top\upload2.bat"') do (
call :Expand Line "%a"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>(
call :Expand Line "REM open heart1"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM open heart1

C:\>goto :eof

C:\>(
call :Expand Line "REM hbc_data"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM hbc_data

C:\>goto :eof

C:\>(
call :Expand Line "REM hbc_data"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM hbc_data

C:\>goto :eof

C:\>(
call :Expand Line "REM cd /home/hbc_data/data/purple_top_data"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM cd /home/hbc_data/data/purple_top_data

C:\>goto :eof

C:\>(
call :Expand Line "REM mput %UploadFile%"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM mput C:\10_23_2015_11_45.csv

C:\>goto :eof

C:\>(
call :Expand Line "REM bye"
 if "!Line:~0,1!" == "[" (goto :eof )  else (echo !Line! 1>>"F:\Purple_Top\uploa
d.ftp" )
)

C:\>set Line=REM bye

C:\>goto :eof

C:\>goto :eof

C:\>echo Content of the ftp file:
Content of the ftp file:

C:\>type "F:\Purple_Top\upload.ftp"
REM open heart1
REM hbc_data
REM hbc_data
REM cd /home/hbc_data/data/purple_top_data
REM mput C:\10_23_2015_11_45.csv
REM bye

C:\>REM ftp.exe -i -v -s:"F:\Purple_Top\upload.ftp" >"upload.log"

C:\>REM del "F:\Purple_Top\upload.ftp"

C:\>move "10_23_2015_11_45.csv" "C:\Purple_Top\Archive"
Access is denied.
        0 file(s) moved.

C:\>REM del "C:\10_23_2015_11_45.csv"

C:\>REM ========================================================================
========

C:\>REM Only functions after this line!

C:\>REM ========================================================================
========

C:\>goto :eof

C:\>

Open in new window


After running the code
Start "Upload" "F:\Purple_Top\upload2.bat"

I get the details
Steven CarnahanNetwork ManagerCommented:
It looks like your getting a permissions error:

C:\>move "10_23_2015_11_45.csv" "C:\Purple_Top\Archive"
Access is denied.
        0 file(s) moved.
anumosesAuthor Commented:
So what is the fix for me to do in the batch file
anumosesAuthor Commented:
@echo off
if exist "F:\purple_top\*.csv" (
copy *.csv C:\purple_top
mv F:\purple_top\*.csv F:\purple_top\archive



setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=C:\Purple_Top
set logfile=upload.log
set ArchiveFolder=%CsvFolder%\Archive
set CsvFile=
if not exist "%ArchiveFolder%" md "%ArchiveFolder%"
rem for /f "delims=" %%a in ('dir /o:d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
for /f "delims=" %%a in ('dir *.csv /b') do set CSVFile=%%a
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)


echo Using '%CsvFile%' as input file.
for %%a in ("%CsvFile%") do (set UploadFile=%%~dpna.csv)

pause

echo Filtering unused columns, writing output to '%UploadFile%'.
powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '%CsvFile%' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -FilePath '%UploadFile%' -Encoding ASCII}"

call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

REM ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"
REM del "%FtpFile%"
move "%CsvFile%" "%ArchiveFolder%"
REM del "%UploadFile%"

REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
REM open heart1
REM hbc_data
REM hbc_data
REM cd /home/hbc_data/data/purple_top_data
REM mput %UploadFile%
REM bye

Open in new window


Please see few lines in the beginning. Copying and moving

I need to add a line to execute the following

Start "Upload" "F:\Purple_Top\upload2.bat"
Steven CarnahanNetwork ManagerCommented:
Start "Upload"  /D F:\Purple_Top\upload2.bat
anumosesAuthor Commented:
or how about adding a line to call purple.bat that is on the F drive purple_top directory?
Steven CarnahanNetwork ManagerCommented:
I guess I should ask the question do you want to use CALL or START?  There are significant differences between the two. The first one you asked (showed) the START command and now you are asking for CALL?
anumosesAuthor Commented:
When I run the purple.bat that has this code

Start "Upload" "F:\Purple_Top\upload2.bat"

Calls upload2.bat

give me an error. Please see attached.
error.jpg
anumosesAuthor Commented:
I guess I should ask the question do you want to use CALL or START?  There are significant differences between the two. The first one you asked (showed) the START command and now you are asking for CALL?

Sorry my question was wrong.

I run the purple.bat.
enclosed in upload2.bat

@echo off
if exist "F:\purple_top\*.csv" (
copy *.csv C:\purple_top
mv F:\purple_top\*.csv F:\purple_top\archive
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=C:\Purple_Top
set logfile=upload.log
set ArchiveFolder=%CsvFolder%\Archive
set CsvFile=
if not exist "%ArchiveFolder%" md "%ArchiveFolder%"
rem for /f "delims=" %%a in ('dir /o:d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
for /f "delims=" %%a in ('dir *.csv /b') do set CSVFile=%%a
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)


echo Using '%CsvFile%' as input file.
for %%a in ("%CsvFile%") do (set UploadFile=%%~dpna.csv)

pause

echo Filtering unused columns, writing output to '%UploadFile%'.
powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '%CsvFile%' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -FilePath '%UploadFile%' -Encoding ASCII}"

call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

REM ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"
REM del "%FtpFile%"
move "%CsvFile%" "%ArchiveFolder%"
REM del "%UploadFile%"

REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
REM open heart1
REM hbc_data
REM hbc_data
REM cd /home/hbc_data/data/purple_top_data
REM mput %UploadFile%
REM bye

Open in new window

anumosesAuthor Commented:
For that error I fixed the encoding ANSI and its ok now.  

@echo off
if exist "F:\purple_top\*.csv" (
xcopy /y F:\purple_top\*.csv C:\purple_top
move F:\purple_top\*.csv F:\purple_top\archive
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)
setlocal enabledelayedexpansion
set FtpFile=%~dp0upload.ftp
set CsvFolder=C:\Purple_Top
set logfile=upload.log
set ArchiveFolder=%CsvFolder%\Archive
set CsvFile=
if not exist "%ArchiveFolder%" md "%ArchiveFolder%"
rem for /f "delims=" %%a in ('dir /o:d /b "%CsvFolder%\*.csv" ^| findstr.exe "^[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]\.csv$"') do (set CsvFile=%CsvFolder%\%%a)
for /f "delims=" %%a in ('dir *.csv /b') do set CSVFile=%%a
if not defined CsvFile (
	echo No csv file found in '%CsvFolder%'.
	exit /b 1
)


echo Using '%CsvFile%' as input file.
for %%a in ("%CsvFile%") do (set UploadFile=%%~dpna.csv)

pause

echo Filtering unused columns, writing output to '%UploadFile%'.
powershell.exe -ExecutionPolicy RemoteSigned -Command "& {Import-Csv -Path '%CsvFile%' | Select-Object -Property 'Sample ID No', 'HGB(g/L)', 'PLT(10^3/uL)' | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object {$_.Replace([char]34 + ',' + [char]34 , ',').Trim([char]34)} | Out-File -FilePath '%UploadFile%' -Encoding ASCII}"

call :ExportDataSection FTP "%FtpFile%"

echo Content of the ftp file:
type "%FtpFile%"

REM ftp.exe -i -v -s:"%FtpFile%" >"%logfile%"
REM del "%FtpFile%"
move "%CsvFile%" "%ArchiveFolder%"
REM del "%UploadFile%"

REM ================================================================================
REM Only functions after this line!
REM ================================================================================
goto :eof
:ExportDataSection
REM *** Reads all lines listed in %1 (section) and writes them to %2 (file name).
REM *** Environment variables will be expanded.
set Section=%~1
set FileName=%~2
if exist "%FileName%" del "%FileName%"
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| C:\Windows\system32\find.exe /n "[%Section%]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto :eof) else (>>"%FileName%" echo !Line!)
)
goto :eof

:Expand
set %1=%~2
goto :eof

REM ================================================================================
REM Only data sections after this line!
REM ================================================================================
[FTP]
REM open heart1
REM hbc_data
REM hbc_data
REM cd /home/hbc_data/data/purple_top_data
REM mput %UploadFile%
REM bye

Open in new window


When I run purple.bat cmd window sits at c:\>
anumosesAuthor Commented:
if exist "F:\purple_top\*.csv" (
xcopy /y F:\purple_top\*.csv C:\purple_top
move F:\purple_top\*.csv F:\purple_top\archive
if not defined CsvFile (
      echo No csv file found in '%CsvFolder%'.
      exit /b 1
)

Copy and move not working I have a feeling.
NVITEnd-user supportCommented:
Try manually run each line. Does that work? Access denied possibly is due to permissions or security rights to the folder. If so, you have to resolve that first.

xcopy /y F:\purple_top\*.csv C:\purple_top
move F:\purple_top\*.csv F:\purple_top\archive
Steven CarnahanNetwork ManagerCommented:
There you may be correct:  https://support.microsoft.com/en-us/kb/240268

Try moving the switch on the xcopy line to after the files

xcopy /y F:\purple_top\*.csv C:\purple_top
to
xcopy F:\purple_top\*.csv C:\purple_top /y
anumosesAuthor Commented:
Running them as separate commands works in cmd window. As you say it may be permission issue or batch behaves differently in windows environment. I tried you second option of moving /y. same result. does not work
NVITEnd-user supportCommented:
Since you're running it via task scheduler, check and adjust the General tab, user account. Use same account as the one used for manually running it. Also may have to set Run with highest privileges.
Steven CarnahanNetwork ManagerCommented:
NVIT has a point about the permissions. I somehow missed the part you were running it in scheduler.  
Couple of other things to keep in mind:
1. If this is running with no user logged on then it won't work because the drives wouldn't be mapped.
2. If the user logged in at the time the task runs doesn't have permissions to the locations or doesn't map drives the same.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anumosesAuthor Commented:
I still have not solved. But will close this ticket as it is not helping but good points to note.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.